rspec关联测试仅适用于一个方向

时间:2010-07-22 06:46:36

标签: ruby-on-rails validation rspec factory-bot

我升级到Rails 3和RSpec 2,我的一个RSpec测试停止了工作:

# Job.rb
class Job < ActiveRecord::Base
  has_one :location
  belongs_to :company

  validates_associated :location
end

# Location.rb
class Location < ActiveRecord::Base 
  belongs_to :job
end

# job_spec.rb
describe Job, "location" do
  it "should have a location" do
    job = Factory(:job)
    location = Factory(:location, :job_id => job.id)

    location.job.should == job      #true
    job.location.should == location #false
  end                                           
end

job.location评估为零,但location.job是正确的。如果我摆脱validates_associated :location,它也可以正常工作。任何人都可以解释为什么这不起作用?

1 个答案:

答案 0 :(得分:2)