0

我有一个应用程序来跟踪客户、工作和工作时间(等等)。我正在使用 rails 3.2.2 和 rspec_rails 2.13.0。我正在关注 Aaron Sumner 的 pdf 书 dailyrailsrspec。

我的关系是,客户可以有很多工作,工作可以有很多时间。

我正在测试我的模型。客户和工作测试都顺利通过。这是我无法上班的工时测试,我才刚刚开始。我无法通过“测试工厂”测试。叹。

工时:

# 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,所以我使用作业工厂创建了一个作业,以从中获取一个 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

4

1 回答 1

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
于 2013-05-30T19:34:15.270 回答