assert_* and assert_not_* 4

Posted by pratik
on Tuesday, March 17

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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:

1
2
3
4
5
6
7
8

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

Make Test::Unit display errors earlier 5

Posted by pratik
on Saturday, March 14

Just a monkey patch to make Test::Unit display errors as soon as they happen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
require 'test/unit'
require 'test/unit/ui/console/testrunner'

class Test::Unit::UI::Console::TestRunner
  def add_fault(fault)
    hax_output(fault)
    @faults << fault
    output_single(fault.single_character_display, 1)
    @already_outputted = true
  end

  def hax_output(fault)
    @io.puts("\n")
    output_single(fault.short_display, 1) # fault.long_display for the full trace
    @io.puts("\n")
  end
end

Useful when your tests take too long to run.