我有一个应用程序可以跟踪客户、工作和工作时间(等等)。我正在使用 rails 3.2.2 和 rspec_rails 2.13.0。我正在关注 Aaron Sumner 的 pdf 书 everydayrailsrspec。
我的关系是,客户可以有很多工作,工作可以有很多时间。
我正在测试我的模型。客户和工作测试都顺利通过。这是我的工时测试,我无法开始工作,而且我才刚刚开始。我无法通过“测试工厂”测试。叹息。
工时工厂:
# spec/factories/hours.rb
FactoryGirl.define do
factory :hour do
job = FactoryGirl.create(:job)
job_id job.id
first_name "John"
last_name "Smith"
hours 8
date_worked "2013-04-27"
description "Did some work"
end
end
Hours 需要一个 job_id,所以我使用 jobs 工厂创建了一个工作,从中获取 job_id。我在我的工作工厂中做同样的事情来获得 customer_id 并且它工作正常。这是“我相信”错误正在犹豫的那一行。它似乎在告诉我它没有看到我的工作工厂。
错误输出(部分)- 查看第一行和最后一行:
/Users/johndcowan/.rvm/gems/ruby-1.9.2-p318/gems/factory_girl-4.2.0/lib/factory_girl/registry.rb:24:in `find': Factory not registered: job (ArgumentError)
from /Users/johndcowan/.rvm/gems/ruby-1.9.2-p318/gems/factory_girl-4.2.0/lib/factory_girl/decorator.rb:10:in `method_missing'
from /Users/johndcowan/.rvm/gems/ruby-1.9.2-p318/gems/factory_girl-4.2.0/lib/factory_girl.rb:71:in `factory_by_name'
from /Users/johndcowan/.rvm/gems/ruby-1.9.2-p318/gems/factory_girl-4.2.0/lib/factory_girl/factory_runner.rb:12:in `run'
from /Users/johndcowan/.rvm/gems/ruby-1.9.2-p318/gems/factory_girl-4.2.0/lib/factory_girl/strategy_syntax_method_registrar.rb:19:in `block in define_singular_strategy_method'
from /Users/johndcowan/MyWebSites/drywall/spec/factories/hours.rb:5:in `block (2 levels) in <top (required)>'
...
输出的第一行有:Factory not registered: job (ArgumentError) 这是否意味着它没有看到我的工作工厂?
Factory 中的第 5 行是:job = FactoryGirl.create(:job)
Jobs factory 以防有帮助:
# spec/factories/jobs.rb
FactoryGirl.define do
factory :job do
# need a customer for the customer_id field
customer = FactoryGirl.create(:customer)
customer_id customer.id
sequence(:name) { |n| "Job#{n}" }
sequence(:address) { |x| "#{x} Main Str" }
city "Cortland"
state "NY"
zip "13045"
sequence(:phone) { |y| "607-75#{y}-1234" }
description "Drywall Work"
end
end
我的规范测试
# spec/models/hour_spec.rb
require 'spec_helper'
describe Hour do
it "has a valid factory" do
FactoryGirl.create(:hour).should be_valid
end
end
感谢任何见解。 --jc
最佳答案
关联定义不正确。尝试使用记录的方式
factory :hour do
job #this is enough
first_name "John"
last_name "Smith"
hours 8
date_worked "2013-04-27"
description "Did some work"
end
关于ruby-on-rails - FactoryGirl Rspec 模型测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16844222/