简体   繁体   English

FactoryGirl Rspec模型测试失败

[英]FactoryGirl Rspec Model Test Failing

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. 我正在使用rails 3.2.2和rspec_rails 2.13.0。 I'm following the pdf book everydayrailsrspec by Aaron Sumner. 我正在关注Aaron Sumner撰写的pdf书籍Everydayrailsrspec。

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. Hours需要一个job_id,因此我使用Jobs工厂创建了一个工作,以从中获取job_id。 I do this same thing in my jobs factory to get a customer_id and it works fine. 我在我的工作工厂中做同样的事情,以获得一个customer_id,并且工作正常。 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? 输出的第一行包含:未注册工厂:job(ArgumentError)这是否表示没有看到我的工作工厂?

Line 5 in the Factory is: job = FactoryGirl.create(:job) 工厂中的第5行是: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 --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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM