我有一个带有两个与 ObjectSpace 相关的方法的 Project 类:
def self.all
ObjectSpace.each_object(self).to_a
end
def self.count
ObjectSpace.each_object(self).count
end
此规范失败:
it "can print all projects" do
Project.all.should eq([@project1, @project2])
end
出现以下错误:
Failure/Error: Project.all.should eq([@project1, @project2])
expected: [#<Project:0x007fd76a815508 @name="Building house", @tasks=[]>, #<Project:0x007fd76a815198 @name="Getting a loan from the Bank", @tasks=[]>]
got: [#<Project:0x007fd7688336b8 @name="Building house", @tasks=[]>, #<Project:0x007fd7688dae40 @name="Building house", @tasks=[]>, #<Project:0x007fd768af4de8 @name="Getting a loan from the Bank", @tasks=[]>, #<Project:0x007fd768af5090 @name="Building house", @tasks=[]>, #<Project:0x007fd76a815198 @name="Getting a loan from the Bank", @tasks=[]>, #<Project:0x007fd76a815508 @name="Building house", @tasks=[]>]
如您所见,这使我的数组中的对象翻了一番,但代码本身工作正常。那么为什么我的测试失败了?