I have an app to track customers, jobs and hours (and such). I'm using rails 3.2.2 and rspec_rails 2.13.0. I'm following the pdf book everydayrailsrspec by Aaron Sumner.
My relationships are, customers can have many jobs, jobs can have many hours.
I'm testing my models. Customers and jobs tests all pass fine. It is my hours tests I cannot get to work and I've only just started on it. I can't get the 'test factory' test to pass. Sigh.
Hours Factory:
# 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 needs a job_id, so I create a job using the jobs factory to get a job_id from it. I do this same thing in my jobs factory to get a customer_id and it works fine. This is the line that 'I believe' the error is balking on. It seems to be telling me it doesn't see my jobs factory.
Error output (partial) - see first and last lines:
/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)>'
...
The first line in output has: Factory not registered: job (ArgumentError) Does this mean it is not seeing my jobs factory?
Line 5 in the Factory is: job = FactoryGirl.create(:job)
Jobs factory in case it helps:
# 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
My spec test
# 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
Thanks for any insight. --jc
The association definition is incorrect. Try to use the documented way
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.