Prev | Current Page 298 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

That is, if the expected exception does not occur, this test will fail. Having
your tests pass only when an exception is thrown is a little confusing at first, so hopefully,
an example will clear up the concept.
Here, we expect our insult method to throw an ActiveRecord::RecordNotFound error,
because we are using an account ID that is not in our data at this time (and we did not use any
exception handling on the insult method within our application):
# test_artest.rb Unit Test example
require 'artest'
require 'test/unit'
CHAPTER 6 ?–  ACTIVE RECORD TESTING AND DEBUGGING 132
class TestArtest < Test::Unit::TestCase
def test_simple
temp = Artest.new
assert_raise ActiveRecord::RecordNotFound do
temp.insult(1000)
end
end
end
Again, because a record for this account does not currently exist in our database, an
ActiveRecord::RecordNotFound error is thrown. This, in turn, allows our test to actually complete
successfully, that is, everything happened as we expected it to. We??™ll cover the various
Active Record exception types later in this chapter.


Pages:
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310