I have some test cases in Ruby version 1.6.8 that are of the form:
class Foo; end
assert_raises(NameError) { Foo.undefinedmethod }
Basically, I want to make sure that a particular operation (i.e. method
invocation) does not work and raises an exception.
Apparently in Ruby 1.8, the exception raised in this case has been
changed to NoMethodError. It is easy enough for me to go and change all of
the occurrences of “assert_raises(NameError)” to
"assert_raises(NoMethodError)", to make this work under 1.8, but I really
would like to have the test cases work under both 1.6.8 and 1.8.
Since the Exception class name is a required parameter for
Test::Unit#assert_raises it looks like I am going to have to create a
constant somewhere like:
NoMethodException = (VERSION == 1.8) ? NoMethodError : NameError
... and then use that constant instead of the actual exception name:
assert_raises(NoMethodException) { … }
This will work, but is messy.
Anyone else see a prettier way to do this?
What would be really nice is if Test::Unit#asser_raises also accepted
zero parameters (true if any exception raised). Any chance of getting
this Nathaniel?
Thanks in advance...
- Warren Brown
Hi –
I have some test cases in Ruby version 1.6.8 that are of the form:
class Foo; end
assert_raises(NameError) { Foo.undefinedmethod }
Basically, I want to make sure that a particular operation (i.e. method
invocation) does not work and raises an exception.
Apparently in Ruby 1.8, the exception raised in this case has been
changed to NoMethodError. It is easy enough for me to go and change all of
the occurrences of “assert_raises(NameError)” to
“assert_raises(NoMethodError)”, to make this work under 1.8, but I really
would like to have the test cases work under both 1.6.8 and 1.8.
Since the Exception class name is a required parameter for
Test::Unit#assert_raises it looks like I am going to have to create a
constant somewhere like:
NoMethodException = (VERSION == 1.8) ? NoMethodError : NameError
... and then use that constant instead of the actual exception name:
assert_raises(NoMethodException) { … }
This will work, but is messy.
How about something like this:
require ‘test/unit’
if VERSION < “1.8”
NoMethodError = NameError
end
class T < Test::Unit::TestCase
def testme
assert_raises(NoMethodError) { 1.blah }
end
end
David
···
On Sat, 24 May 2003, Warren Brown wrote:
–
David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav