2014-05-08 56 views
0

我想比較兩個數組比較2個陣列,但是assert_equal引發錯誤:內容相等

No visible difference in the Array#inspect output. 
You should look at the implementation of #== on Array or its members. 

這裏是我的測試:

describe Post do 
    before do 
     @draft = Post.new('test-draft', draft = true) 
     @post = Post.new('2014-10-31-test-post') 
    end 

    describe '.drafts' do 
     it 'returns an array of unpublished posts' do 
      actual = Post.drafts   
      assert_equal [@draft], actual 
     end 
    end 
end 

這裏是我的兩個數組,它們是相同的在內容上,但有不同的內存位置。

[ 
    [0] #<Post:0x0000000851a178 @file_name="test-draft", @draft=true> 
] 
[ 
    [0] #<Post:0x0000000851a538 @file_name="test-draft", @draft=true> 
] 

如何比較我的兩個數組?

回答

2

這聽起來像安置自己的類不有自己的==運算符,所以沒有兩個Post對象可以相等。我想你會想給Post一些平等的想法。

1

既然你想看,如果值是正確的,你可以實現類似方法的助手將實現類似

a = [1,2,3] 
b = [1,2,3] 

((a - b) + (b - a)).empty? 
#=> true 

def arrays_equal_values?(a, b) 
    ((a - b) + (b - a)).empty? 
end