Prev | Current Page 288 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"


How to Write Good Unit Tests
Unit testing in Ruby, as most Ruby tasks are, is actually very simple. It relies on the Unit::Test
library maintained by Nathaniel Talbott and comes as a part of the core Ruby installation, so it
should always be available for all your Ruby applications.
As its documentation states, ???The general idea behind unit testing is that you write a test
method that makes certain assertions about your code, working against a test fixture.??? So it
only make sense that the key methods in your unit tests will be assertions, and the data that
generally is used within your tests is defined in data sets called fixtures.
Before we go into detail about each of these, though, it??™s probably easiest to show a basic
example, so we have something to work from throughout the rest of this chapter. Obviously,
in order to write tests, we first need some code that we want to test against. Let??™s start with the
following generic example, which does very little but return a few vanity messages and do some
basic calculations:
# artest.


Pages:
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300