Using and counting assertions from a module

J

Joe Yakich

------=_NextPart_000_005C_01C5A716.73402570
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

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


------=_NextPart_000_005C_01C5A716.73402570--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top