10

I can't use the match_array to match agains an empty array 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 the match_array([]) by be_empty, my code works. Also, as pointed by @PeterAlfvin, change custom_text_locations initialization on the subject to [] seems to work.

This is my method:

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

Question: What is wrong with my test?

3
  • That's odd. If you replace subject.custom_text_locations with [] does it pass? If you print subject.custom_text_locations.inspect before the expect, what do you get? Commented Feb 11, 2014 at 16:59
  • Also, what version of RSpec are you using? Commented Feb 11, 2014 at 17:26
  • @PeterAlfvin You got it right, if I replace by [] it works. rspec version 2.14.7. I'm adding the method also, since it seems to be involved the problem. Commented Feb 11, 2014 at 18:06

1 Answer 1

26
+50

The code you posted is not the code that generated the error you posted.

The error you posted includes the incorrect to be match_array([]) which is very different from the correct to match_array([]) you have in your posted code.

Sign up to request clarification or add additional context in comments.

4 Comments

Are you a seer or something? :O
I'm unaccepting your answer then I can award you some extra points with a bounty hunter.
Hardly a seer, more like a blind man. ;-) I'd been staring at and missing that difference for longer than I care to admit!
@fotanus Thanks, that's generous. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.