Search code examples
ruby-on-railsrspecrspec2rspec-rails

The data must be identical, and the test should be successful. Data are identical, but the test failed


I am testing method last_photo:

def last_photo
  @last_photo ||= user_updates.latest.where("photo_front IS NOT NULL and photo_front != ''").first.try(:photo_front)
end

Spec:

context "instance method" do
  let(:user) { create :user }

  context "last photo" do
    before { create_list(:user_update, 3, user: user) }
    let(:user_updates){ user.user_updates }

    describe "#last_photo" do
      subject { user.last_photo }

      it { should eq user_updates.latest.first.photo_front }
    end
  end
end

the test should be successful. But there are strange error.

Attached GIST.


Solution

  • The answer is pretty simple really:

    expected: #<PhotoUploader:0x00000007e34868 ...
    got: #<PhotoUploader:0x00000007ebc100 ...
    

    The values might be the same, but the objects are different in memory. Since you're doing a comparison on the objects, rspec expects the objects to be the exact same.

    Now, user.user_updates and user_updates are two different variables in memory. You should do a comparison on the values.