I have a method which returns an array. I need to test it using rspec. Is there any method with which we can test like:
def get_ids
####returns array of ids
end
subject.get_ids.should be_array
or
result = subject.get_ids
result.should be an_instance_of(Array)
Well, depends on what exactly you're looking for.
To check if the returned value is an array (be_an_instance_of):
expect(subject.get_ids).to be_an_instance_of(Array)
or to check if the returned value matches the content of expected array (match_array):
expect(subject.get_ids).to match_array(expected_array)
Update:
As mentioned in Patrick's comment, to check equivalence match of array use eq
:
expect(subject.get_ids).to eq(expected_array)
For identity match of array use equal
:
expect(subject.get_ids).to equal(expected_array)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With