我有一个应用程序来跟踪客户,工作和时间(等等)。我使用的是rails 3.2.2和rspec_rails 2.13.0。我每天都在关注亚伦·萨姆纳的pdf书《铁路规范》。
我的关系是,客户可以有很多工作,工作可以有很多小时。
我在测试我的模型。客户和工作测试都通过了测试。这是我的时间测试,我不能去工作,我才刚刚开始。我不能让“测试工厂”测试通过。叹一口气。
工厂小时数:
# 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小时需要一个job_id,所以我使用作业工厂创建了一个作业,以便从中获取一个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)这是否意味着它没有看到我的作业工厂?
工厂中的第5行是: job = FactoryGirl.create(:job)
工作工厂,如果它有帮助的话:
# 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
发布于 2013-05-31 03:34:15
关联定义不正确。尝试使用文档化的方式
factory :hour do
job #this is enough
first_name "John"
last_name "Smith"
hours 8
date_worked "2013-04-27"
description "Did some work"
endhttps://stackoverflow.com/questions/16844222
复制相似问题