I'm having trouble using assert(). I have been twiddling with it by putting
it either in a module (which throws a NameError) or in a class (which
doesn't complain, but doesn't include the assert() in the summary line that
is printed at the end of the test run).
Here's the code skeleton I have been using with a class:
# library.rb
class MyLibrary
require 'test/unit'
require 'test/unit/assertions'
include Test::Unit::Assertions
def my_library_method
# code.
assert(some_assertion_expression_here)
end
end
# tests.rb
require 'test/unit'
require 'library.rb'
class TC1 << Test::Unit::TestCase
def test_method
l = MyLibrary.new()
l.my_library_method
# more code
end
end
Here's the output:
1 tests, 0 assertions, 0 failures, 0 errors
I was expecting:
1 tests, 1 assertions, 0 failures, 0 errors
If I try to do something similar using a module instead of a class for
MyLibrary (code and results below) I get a NameError.
# library.rb
module MyLibrary
require 'test/unit'
require 'test/unit/assertions'
include Test::Unit::Assertions
def my_library_method(arg)
# code...
assert(some_assertion_expression_here)
end
end
# tests.rb
require 'test/unit'
require 'library.rb'
class TC1 << Test::Unit::TestCase
include MyLibrary
def test_method
my_library_method
# more code
end
end
Here's the output:
NoMethodError: undefined method `assert' for Login:Module
I asked this question on the Ruby Watir mailing list, and Bret Pettichord
said this:
This is a hard problem, that i'm still looking for a good answer.
I've spent time investigating it. The problem is that there are two
different versions of add_assertion,
the method that updates the assertion count.
One of them is in Test::Unit::TestCase -- and it is what you get if you do
what Warren did below.
The other is is Test::Unit::Assertions -- and this is the one that doesn't
actually count your assertions.
The solution? I don't know. I've been meaning to write up this problem on
ruby-talk and see if i could get
some help there with the problem.
Does anyone have any ideas about a workaround or something I should try
instead?
Joe