Rails Rspec工厂功能测试:未注册

Rails Rspec工厂功能测试:未注册,rspec,ruby-on-rails-5,capybara,Rspec,Ruby On Rails 5,Capybara,rails5.0.4ruby 2.5.1rspec3.7capybara 我需要一些关于创建工厂以及如何在我的功能规格中使用它们的帮助。我总是遇到同样的问题,当我认为我已经解决了,我又遇到了同样的问题 型号: customer.rb has many jobs job.rb belongs to customer has_many hours hour.rb belongs to job 工厂: #customer.rb FactoryGirl.define do factor

rails5.0.4
ruby 2.5.1
rspec3.7
capybara

我需要一些关于创建工厂以及如何在我的功能规格中使用它们的帮助。我总是遇到同样的问题,当我认为我已经解决了,我又遇到了同样的问题

型号:

customer.rb
 has many jobs

job.rb
 belongs to customer
 has_many hours

hour.rb
 belongs to job
工厂:

#customer.rb
FactoryGirl.define do
  factory :customer, :class => Customer do
    name "abc construction"
    contact "John"
    street "101 Main street"
    city "Anywhere"
    state "NY"
    zip "12345"
    phone "123-555-4567"
  end
end

#job.rb
FactoryGirl.define do
  factory :job, :class => Job do
    customer_id
    street "55 first str"   
    city "Anywhere"
    state "NY"
    zip "12345"  
    phone "123-456-7890"
    created_at Time.now
    updated_at Time.now
    description  "Test Job"
    name "house repairs"    
  end
end

# hour.rb
FactoryGirl.define do
  factory :hour, :class => Hour do
    job_id
    date_worked Date.today  
    hours 1
    description "test"   
  end
end
小时功能规格

# hours_spec.rb
context "edit form" do
    before(:each) do
      customer=FactoryGirl.create(:customer)
      job=FactoryGirl.create(:job, :customer_id => customer.id)
      hours=FactoryGirl.create(:hour, :job_id => job.id)

      visit edit_hour_path(:id => hours.id)
    end
    scenario "update messages shows" do
      within('form') do
        fill_in 'hour_description', with: 'testing'
      end
      click_button("Update Hours")
      expect(page).to have_content("You updated the record")
    end
  end
运行小时规格测试

rspec spec/features/hours_spec.rb
...
.....Capybara starting Puma...
* Version 3.9.1 , codename: Private Caller
* Min threads: 0, max threads: 4
* Listening on tcp://127.0.0.1:44465
.DEPRECATED: Capybara::Helpers::normalize_whitespace is deprecated, please update your driver
.F

Failures:

  1) Hours edit form update messages shows
     Failure/Error: job=FactoryGirl.create(:job, :customer_id => customer.id)

     ArgumentError:
       Trait not registered: customer_id
     # ./spec/features/hours_spec.rb:58:in `block (3 levels) in <top (required)>'

Finished in 0.86646 seconds (files took 1.24 seconds to load)
8 examples, 1 failure
rspec规格/特性/小时数\u规格rb
失败:
1) 小时编辑表单更新消息显示
失败/错误:job=FactoryGirl.create(:job,:customer\u id=>customer.id)
参数错误:
未注册:488
#./spec/features/hours_spec.rb:58:in‘block(3层)in’
我做错了什么?正确的方法是什么


谢谢你的帮助

您需要向工厂方法传递散列,而不是分配局部变量

job=FactoryGirl.create(:job, customer_id: customer.id) 

虽然你在工作和客户之间有一种归属关系,但你应该在工厂女孩(现在称为工厂机器人)中寻找这种关系并且在您的工厂中有
客户
而不是客户id

FactoryGirl最近被重命名为FactoryBot,并且由于与该日期相关的问题,取消了对为属性指定静态值的支持(工厂中的所有日期/时间将始终指定为读取代码的时间/日期,而不是代码实际运行的时间。您应该更新,并在工厂中使用关联,而不是直接分配ID


#customer.rb
FactoryBot.define do
  factory :customer, :class => Customer do
    name { "abc construction" }
    contact { "John" }
    street { "101 Main street" }
    city { "Anywhere" }
    state { "NY" }
    zip { "12345" }
    phone { "123-555-4567" }
  end
end

#job.rb
FactoryBot.define do
  factory :job do # class defaults to one matching the factory name
    customer # association type defaults to one matching the name
    street { "55 first str" }   
    city { "Anywhere" }
    state { "NY" }
    zip { "12345" }  
    phone { "123-456-7890" }
    # created_at and updated_at are handled automatically 
    # created_at { Time.now }
    # updated_at { Time.now }
    description  { "Test Job" }
    name { "house repairs" }    
  end
end
job=FactoryBot.create(:job)#这将自动创建关联客户


job=FactoryBot.creatE(:job,customer:FactoryBot.creatE(:customer,name:'abc'))#将使用作为关联客户传递的客户

谢谢,但这不起作用。我应该校对我的帖子,我必须复制粘贴我尝试的内容。我正在使用'job=FactoryGirl.creatE(:job,:customer\u id=>customer.id)“我编辑了我的第一篇帖子。谢谢!我将致力于改用FactoryBot。与此同时,我找到了如何让我的代码与FactoryGirl协同工作的方法,就像你指出的那样。只需在我的
工作工厂中使用
customer
而不是
customer\u id
”`@JohnCowan是的,就是使用我提到的关联在另一个答案中,并在这个答案中显示感谢您为我指出了正确的方向。非常感谢。
job=FactoryGirl.create(:job, customer_id: customer.id) 

#customer.rb
FactoryBot.define do
  factory :customer, :class => Customer do
    name { "abc construction" }
    contact { "John" }
    street { "101 Main street" }
    city { "Anywhere" }
    state { "NY" }
    zip { "12345" }
    phone { "123-555-4567" }
  end
end

#job.rb
FactoryBot.define do
  factory :job do # class defaults to one matching the factory name
    customer # association type defaults to one matching the name
    street { "55 first str" }   
    city { "Anywhere" }
    state { "NY" }
    zip { "12345" }  
    phone { "123-456-7890" }
    # created_at and updated_at are handled automatically 
    # created_at { Time.now }
    # updated_at { Time.now }
    description  { "Test Job" }
    name { "house repairs" }    
  end
end