Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec to test whether ruby method returns an array

Tags:

ruby

rspec

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)
like image 789
rubyist Avatar asked Oct 28 '14 07:10

rubyist


1 Answers

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)
like image 143
Surya Avatar answered Nov 23 '22 15:11

Surya