Prev | Current Page 308 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

rb Unit Test example
require 'artest'
require 'test/unit'
class TestArtest < Test::Unit::TestCase
def test_simple
temp = Artest.new
assert_send [temp, :appreciate, 1]
end
end
flunk
The flunk method will always cause a failure within your test. The method accepts one optional
parameter, which is a message to display at the point of failure.
The important thing to remember with the flunk method is that your test will stop execution
at the first flunk method it encounters (unless you handle the exceptions within your tests).
So you can use flunk as a break point for your tests and even assign a custom error message to
help you narrow down especially tricky problems.
In our example, we use two different messages to help us quickly determine which bit of
our test is really being executed:
# test_artest.rb Unit Test example
require 'artest'
require 'test/unit'
class TestArtest < Test::Unit::TestCase
def test_simple
temp = Artest.new
if temp.appreciate(1).include?("Kevin")
CHAPTER 6 ?–  ACTIVE RECORD TESTING AND DEBUGGING 138
flunk "Kevin was found in our text!"
else
flunk "Kevin was not found in our text!"
end
end
end
So far, we??™ve just been focusing on showing examples of each of the unit test assertion
methods.


Pages:
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320