简体   繁体   中英

rspec to test whether ruby method returns an array

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM