Skip to content
mynyml edited this page Sep 13, 2010 · 7 revisions

Nano Extensions

DIY Assertions

If you want to call assert from somewhere else in the stack, you can use its file_name and line_number arguments to correctly report where the failure comes from. Those are particularily helpful for writing custom assertions.

 Nanotest.assert( failure_message, file_name, line_number ) { bool }

Here’s how you can get that info:


  def assert_includes(collection, item)
    file = caller.first.split(':')[0]
    line = caller.first.split(':')[1]
    msg = "expected #{collection.inspect} to include #{item.inspect}"
    Nanotest.assert(msg, file, line) { collection.include?(item) }
  end

  assert_includes([1,2,3], 4) #line 33
  #=> (examples.rb:033) expected [1, 2, 3] to include 4

  def refute(message="assertion failed")
    file = caller.first.split(':')[0]
    line = caller.first.split(':')[1]
    Nanotest.assert(message, file, line) { not yield }
  end

  refute { 'foo'.is_a?(String) } #line 66
  #=> (examples.rb:066) assertion failed

rake test

Nanotest exits with proper status (0 when successful, 1 otherwise). Use it when running your tests with rake:


  task(:default => "test")

  desc "Run all tests"
  task(:test) do
    exit system("ruby -rubygems -I.:lib test/test_mylib.rb")
  end
Clone this wiki locally