Very simple spec...
@post.user.should == @user
Spec fails even though both objects are identical in every way except their object_id. ActiveRecord objects should equal (==) if their id's are the same. The objects are being created using factory_girl. I've confirmed that neither object is a ".new_record?". Comparing @post.user.id and @user.id works.
Whats more is the behavior is not consistent. These tests were working and now they fail despite no changes.
- Rails 3.1
- rspec-2.6
- factory_girl-2.0.5
- factory_girl_rails-1.1.0
- jruby 1.6.4 (ruby-1.9.2-p136)
- windows7
I typically use spork but this happens without spork also.
Some more details:
The offending line of code appears to be the following in ActiveRecord::Base
def ==(comparison_object)
super ||
comparison_object.instance_of?(self.class) &&
id.present? &&
comparison_object.id == id
end
Specifically, the "instance_of?" check is failing when it shouldn't be. I checked the class hierarchy of both objects. However, when I check the class.id of each object, they are not equal.
Furthermore, the behavior is dependant on which command I run...
jruby -S bundle exec rspec spec (FAILS)
jruby -S bundle exec rspec spec\models (PASSES)
jruby -S bundle exec rspec spec\models\post_spec.rb (PASSES)
Setting cache_classes=true in my environments/test.rb file seems to fix this, but I don't think this should be necessary.