This post originated from an RSS feed registered with Ruby Buzz
by Guy Naor.
Original Post: Testing: DRYing the Asserting of Array Similarity
Feed Title: Famundo - The Dev Blog
Feed URL: http://devblog.famundo.com/xml/rss/feed.xml
Feed Description: A blog describing the development and related technologies involved in creating famundo.com - a family management sytem written using Ruby On Rails and postgres
I decided it's time to DRY up array similarity checking. Using assertequal on arrays is a so-so solution, given that you don't always know if the order in the array is the same. Returning an array from a hasmany collection has a non-deterministic order by default. So asserting that what we get from a collection is a bit painfull, and require the code above to make absolutely sure we got what we wanted.
To make my life easier and DRYer, I added the following function to the test/test_helper.rb file:
def assert_array_similarity(expected,actual,message=nil)full_message=build_message(message,"<?> expected but was\n<?>.\n",expected,actual)assert_block(full_message){(expected.size==actual.size)&&(expected-actual==[])}end