数据必须相同,测试应该是成功的。数据相同,但测试失败

时间:2014-03-25 13:34:54

标签: ruby-on-rails rspec rspec2 rspec-rails

我正在测试方法last_photo

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

规格:

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

测试应该是成功的。但是有一些奇怪的错误。

附上GIST

1 个答案:

答案 0 :(得分:2)

答案非常简单:

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

值可能相同,但内存中的对象不同。由于您正在对对象进行比较,因此rspec期望对象完全相同。

现在,user.user_updatesuser_updates是内存中的两个不同变量。你应该对这些值进行比较。