rails 5.0.4
ruby 2.5.1
rspec 3.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
如果将我的工作工厂更改为具有customer_id的值,则会收到类似的错误,但有一个数字(488)。也许它会显示为customer_id?
FactoryGirl.define do
factory :job, :class => Job do
customer_id FactoryGirl.create(:customer)
...
end
rspec spec/features/hours_spec.rb
Failures:
1) Hours edit form update messages shows
Failure/Error: job=FactoryGirl.create(:job, :customer_id => customer.id)
ArgumentError:
Trait not registered: 488
# ./spec/features/hours_spec.rb:58:in `block (3 levels) in <top (required)>'
我在做什么错?正确的方法是什么?
感谢您的帮助!
答案 0 :(得分:0)
您需要将哈希传递给工厂方法,而不是分配局部变量
job=FactoryGirl.create(:job, customer_id: customer.id)
尽管由于您在工作和客户之间建立了belongs_to关联,所以您应该在factory_girl(现在称为factory bot)中进行关联,并在工厂中拥有customer
,而不是customer_id https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#associations
答案 1 :(得分:0)
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,客户:FactoryBot.create(:customer,名称:'abc'))#将使用作为关联客户传递的客户