MonoForge

duff / assert_arrays_equal

assert_arrays_equal

Public

Rails plugin for prettied up test output when you're asserting that 2 arrays are equal

4 filesupdated Jun 18, 2026

README

= AssertArraysEqual plugin for Rails

Here's a simple test:

def test_relationships wellspring = churches(:wellspring) cleaning_crew = Group.create!(:name => "Cleaning Crew", :church => wellspring) assert_equal([cleaning_crew], wellspring.groups) end

The problem is that when this test fails, I get a ridiculous amount of output and it's difficult to quickly determine why the test is failing. Here's the output I get:

  1. Failure: test_relationships:10 <[#<Group:0x3492918 @attributes= {"name"=>"Cleaning Crew", "church_id"=>1, "id"=>32, at top level in "created_at"=>Thu Mar 01 15 at line 56 @church= at top level in #<Church at line 0 @attributes= {"name"=>"Wellspring Community Church", "account_owner_id"=>"1", "timezone"=>"Alaska", "subdomain"=>"wellspring", "id"=>"1", "tz_name"=>nil}, @groups= at top level in #<Group at line 0 @errors= at top level in #<ActiveRecord::Errors at line 0 @new_record=false>]> expected but was <[#<Group:0x348c0a4 @attributes={"name"=>"Elders", "church_id"=>"1", "id"=>"1", "created_at"=>"2007-03-01 10:01:58"}>, #<Group:0x348c07c @attributes={"name"=>"Children's Ministry", "church_id"=>"1", "id"=>"4", "created_at"=>"2007-03-01 10:01:58"}>, #<Group:0x348c054 @attributes={"name"=>"Cleaning Crew", "church_id"=>"1", "id"=>"32", "created_at"=>"2007-03-01 15:56:38"}>]>.

1 tests, 1 assertions, 1 failures, 0 errors

This plugin allows me to call #assert_arrays_equal rather than #assert_equal. So the test now looks like this:

def test_relationships wellspring = churches(:wellspring) cleaning_crew = Group.create!(:name => "Cleaning Crew", :church => wellspring) assert_arrays_equal([cleaning_crew], wellspring.groups, :name) end

The #assert_arrays_equal method allows you to specify which method you'd like to be called on the objects in your arrays for the output message. It still asserts that the arrays are actually equal. The failure error message just uses the method you specify. In the test above, I specified the :name method because I want Group.#name to be called rather than Group#inspect. If you don't pass in a method, it defaults to #to_s.

So now the output looks like this:

  1. Failure: test_relationships(GroupTest) method assert_arrays_equal in test_unit_extras.rb at line 8 method test_relationships in group_test.rb at line 11 <"Cleaning Crew"> expected but was <"Elders, Children's Ministry, Cleaning Crew">.

1 tests, 1 assertions, 1 failures, 0 errors

== Additional Info

Author: Duff OMelia Contact: dj@omelia.org License: MIT

Copyright (c) 2007 Duff OMelia, released under the MIT license