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

发布于 2024-09-11 02:15:30 字数 718 浏览 5 评论 0原文

我升级到 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 计算结果为 nil,但 location.job 是正确的。如果我摆脱 validates_linked :location ,它也可以正常工作。谁能解释为什么这不起作用?

I upgraded to Rails 3 and RSpec 2 and one of my RSpec tests stopped working:

# 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 evaluates to nil but location.job is correct. It also works fine if I get rid of validates_associated :location. Can anyone explain why this doesn't work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷遇一生 2024-09-18 02:15:30

工作已经在记忆中了。或者在创建位置后重新加载它,或者使用 lambda/expect。例如:

describe Job, "location" do
  it "should have a location" do
    job = Factory(:job)
    location = Factory(:location, :job_id => job.id)
    job.reload
    location.job.should == job      #true
    job.location.should == location #false
  end 

  it "should have a location" do
    job = Factory(:job)

    expect {
      location = Factory(:location, :job_id => job.id) 
    }.to change(job, :location).to(location)
    lambda {
      location = Factory(:location, :job_id => job.id) 
    }.should change(job, :location).to(location)

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

更多信息在这里:http://rspec。 rubyforge.org/rspec/1.3.0/classes/Spec/Matchers.html#M000168

job is already on memory. or you reload it after creating location, or use lambda/expect. eg:

describe Job, "location" do
  it "should have a location" do
    job = Factory(:job)
    location = Factory(:location, :job_id => job.id)
    job.reload
    location.job.should == job      #true
    job.location.should == location #false
  end 

  it "should have a location" do
    job = Factory(:job)

    expect {
      location = Factory(:location, :job_id => job.id) 
    }.to change(job, :location).to(location)
    lambda {
      location = Factory(:location, :job_id => job.id) 
    }.should change(job, :location).to(location)

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

more info here: http://rspec.rubyforge.org/rspec/1.3.0/classes/Spec/Matchers.html#M000168

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文