Skip to content

Instantly share code, notes, and snippets.

@willnet
Created November 22, 2012 01:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willnet/4128828 to your computer and use it in GitHub Desktop.
Save willnet/4128828 to your computer and use it in GitHub Desktop.
attributes custom matcher for minitest
# -*- coding: utf-8 -*-
class EqualAttributesMatcher
def initialize(attrs)
@attrs = attrs
end
def matches?(model)
@model = model
@attrs.all? { |k,v| @model.send(k) == v }
end
def failure_message_for_should
left_diff = @attrs.reject {|k,v| @model.send(k) == v}
right_diff = left_diff.each_with_object({}) {|(k,_), hash| hash[k] = @model[k] }
"expected #{@attrs} but diff -#{left_diff} +#{right_diff}}"
end
def failure_message_for_should_not
"expected not equal #{@attrs} but equal"
end
end
MiniTest::Unit::TestCase.register_matcher EqualAttributesMatcher, :have_equal_attributes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment