Ruby 类看起来是相同的,但并不被认为是相等的
两个物体怎么可能看起来相同,但实际上却不相同?
让 pickle_step 运行代码“model!(owner).send(association).should == model!(target)”
在标签 (cucumber --tags @thisonescenario) 中运行时,所有者和目标相同并且测试通过。
当与其余场景(黄瓜)一起运行时,所有者和目标不同。
经过检查(rdebug)后,代码显示这些类并不相同,尽管它们实际上看起来是相同的。 Cucumber 步骤的输出如下:
expected: #<Content _id: content_1, _type: nil>
got: #<Content _id: content_1, _type: nil> (using ==)
Diff: (RSpec::Expectations::ExpectationNotMetError)
注意: == 由 Mongo 库重载,代码如下:
def ==(other)
self.class == other.class &&
attributes["_id"] == other.attributes["_id"]
end
_id 比较为 true。 self.class == other.class 是 false。
检查类属性(例如后代、祖先等)表明它们是相同的。
有什么想法吗?
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't 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果使用 === 而不是 == 来比较类名会怎样?
类似的东西
What if you use === instead == to compare the classes names?
something like