我有一个应用程序来跟踪客户,工作和工作时间(等)。我正在使用rails 3.2.2和rspec_rails 2.13.0。我正在关注Aaron Sumner的pdf book 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
小时需要一个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)>'
...
输出中的第一行有:工厂未注册: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
答案 0 :(得分:1)
关联定义不正确。尝试使用记录的方式
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