Skip to content

Instantly share code, notes, and snippets.

View gist:287675
# In reply to http://www.ultrasaurus.com/sarahblog/2009/08/ruby-unit-test-frameworks/
# Put this in test_helper.rb to compare two arbitrarily nested structures.
# Usage: compare_objects(o1, o2). Raises an exception if it finds any differences.
# recursivly compare two objects. raise if a difference was found, return if not.
def compare_objects(wanted, actual, path=[])
if wanted.kind_of?(Hash)
raise "Objects differ at #{path.join(".")}: extra keys in wanted: #{(wanted.keys - actual.keys).inspect}" if (wanted.keys - actual.keys).length > 0
raise "Objects differ at #{path.join(".")}: extra keys in actual: #{(actual.keys - wanted.keys).inspect}" if (actual.keys - wanted.keys).length > 0
wanted.keys.sort.each do |key|