Skip to content

Instantly share code, notes, and snippets.

@willnet
Created November 22, 2012 01:06
Show Gist options
  • Select an option

  • Save willnet/4128831 to your computer and use it in GitHub Desktop.

Select an option

Save willnet/4128831 to your computer and use it in GitHub Desktop.
valid attribute custom matcher for minitest
class ValidAttributeMatcher
def initialize(attr_name)
@attr_name = attr_name
end
def matches?(subject)
@subject = subject
@subject.valid?
@subject.errors[@attr_name].empty?
end
def failure_message_for_should
"expected to valid ##{@attr_name} but #{@subject.errors[@attr_name]}"
end
def failure_message_for_should_not
"expected to invalid ##{@attr_name} but valid"
end
end
MiniTest::Unit::TestCase.register_matcher ValidAttributeMatcher, :have_valid_attribute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment