让控制器规范通过

时间:2015-11-25 12:00:25

标签: ruby-on-rails rspec rspec-rails

我正在尝试深入测试,并且我遇到了让这个节目控制器通过的问题。我已经完成了我能想到的所有调整。

spec/controllers/contacts_controller_spec.rb

describe "GET #show do" do
before do
  @contact = FactoryGirl.attributes_for(:contact)
end

it "assigns the requested contact to @contact" do
  get :show, user_id: user.id, id: contact
  expect(:contact).to be(contact)
end
end

这是工厂

FactoryGirl.define do
  factory :contact do
    full_name      { Faker::Name.name}
    email          { Faker::Internet.email}
    sex            "male"
    date_of_birth  "19/02/1993"
    phone_number   { Faker::PhoneNumber.phone_number }
    user           { create :user }
    address        { Faker::Address.street_address }
    country        { Faker::Address.country }
    state          { Faker::Address.state }
    city           { Faker::Address.city }
    postal_code    { Faker::Address.postcode }
end

我的节目控制器

def show
    @contact = Contact.find(params[:id])
 end

1 个答案:

答案 0 :(得分:1)

Factory Girl的function remove_msg_filter($msg, $msg_code, $this){ if(is_checkout()){ return ""; } return $msg; } add_filter('woocommerce_coupon_message','remove_msg_filter',10,3); 返回下一个工厂序列的属性。它不会为您创建对象。因此,当您使用联系人发出get请求时,它可能返回nil,因为它只创建了属性而不是数据库条目。

因此,当您测试节目时,请先创建您的联系人,然后检查您的节目是否正在返回正确的节目。

attributes_for

如果您尝试将before do @contact = FactoryGirl.create(:contact) end user_id作为获取请求参数传递,那么它将是这样的:

id
相关问题