Rspec match_array error for empty array?

I cannot use match_arrayto combine an empty array again in one of my tests. I have the following message:

Failure/Error: expect(subject.custom_text_locations).to be match_array([])

   expected #<RSpec::Matchers::BuiltIn::MatchArray:105810100> => #<RSpec::Matchers::BuiltIn::MatchArray:0xc9d1168 @expected=[]>
        got #<Array:105810180> => []

Here is my test:

context 'when there is no custom text locations' do
  subject { create(:service, custom_text_locations: nil) }
  it 'returns empty list' do
    expect(subject.custom_text_locations).to match_array([])
  end 
end 

If I change match_array([])to be_empty, my code works. Also, as @PeterAlfvin points out, changing the custom_text_locationsinitialization of the object to []seems to work.

This is my method:

def custom_text_locations
  self[:custom_text_locations] || []
end

Question: What is wrong with my test?

+4
source share
1 answer

The code you submitted is not the code that generated the error you sent.

The error you entered contains an incorrect one to be match_array([]), which is very different from the correct to match_array([])one that you have in your published code.

+9

Source: https://habr.com/ru/post/1526382/


All Articles