assert_* and assert_not_*
Published about 5 years ago

Not sure I like this or not, but I’m gonna give it a shot in the “real world” nevertheless.

module AssertionExtensions
  def method_missing(method_id, *arguments, &block)
    return super unless method_id.to_s =~ /^assert_(not_)?(.*)$/

    method = "#{$2}?"
    object = arguments.first

    if $1
      arguments.each do |object|
        assert ! object.send(method), "#{method} is not false for #{object}"
      end
    else
      arguments.each do |object|
        assert object.send(method), "#{method} is not true for #{object}"
      end
    end
  end
end

class ActiveSupport::TestCase
  include AssertionExtensions
end

Now using this in your tests:

def test_is_admin
  # [people(:admin), people(:superuser)].each {|p| assert p.admin?}
  assert_admin people(:admin), people(:superuser)

  # assert ! people(:foo).admin?
  assert_not_admin people(:foo)
end