开发者

Ruby classes seem to be the same but not considered equal

开发者 https://www.devze.com 2023-03-20 14:08 出处:网络
How can two objects seem to be identical but are not? Have pickle_step that runs code \"model!(owner).send(association).should == model!(target)\"

How can two objects seem to be identical but are not?

Have pickle_step that runs code "model!(owner).send(association).should == model!(target)"

When run in a tag (cucumber --tags @thisonescenario), owner and target are the same and the test passes.

When ran with rest of scenarios (cucumber), the owner and target are not the same.

After inspection (rdebug) the code says the classes aren'开发者_如何学Ct the same even though they really seem to be. Output of cucumber step is as follows:

  expected: #<Content _id: content_1, _type: nil>
       got: #<Content _id: content_1, _type: nil> (using ==)
  Diff: (RSpec::Expectations::ExpectationNotMetError)

Note: == overloaded by Mongo library with code below:

def ==(other)
  self.class == other.class &&
  attributes["_id"] == other.attributes["_id"]
end

The _id comparison is true. The self.class == other.class is false.

Inspection of classes properties such as descendants, ancestors, etc. show they are the same.

Any ideas?


What if you use === instead == to compare the classes names?

something like

other === self  && ...
0

精彩评论

暂无评论...
验证码 换一张
取 消