rspec 关联测试仅适用于一个方向
我升级到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
工作已经在记忆中了。或者在创建位置后重新加载它,或者使用 lambda/expect。例如:
更多信息在这里: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:
more info here: http://rspec.rubyforge.org/rspec/1.3.0/classes/Spec/Matchers.html#M000168