1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Running the Test

By

Running the Test in Rails

Next, we'll go ahead and run the test. As expected, the test fails and gives us the message from the "must have a body" test.

$ rake test:units
/usr/bin/ruby1.8 -Ilib:test "/usr/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/unit/post_test.rb"
Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
Started
F.
Finished in 0.12814 seconds.

1) Failure:
test_must_have_a_body(PostTest)
[./test/unit/post_test.rb:11:in `test_must_have_a_body'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `__send__'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:60:in `run']:
Post was saved without a body.
<false> is not true.

2 tests, 2 assertions, 1 failures, 0 errors
rake aborted!
Command failed with status (1): [/usr/bin/ruby1.8 -Ilib:test "/usr/lib/ruby...]

(See full trace by running task with --trace)

Now, to implement the validation. A simple one-line addition to the Post model is all it takes.

class Post < ActiveRecord::Base
<b> validates_presence_of :body</b>
end

Run the tests again and make sure all the tests pass.

$ rake test:units
/usr/bin/ruby1.8 -Ilib:test "/usr/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/unit/post_test.rb"
Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
Started
..
Finished in 0.108586 seconds.

2 tests, 2 assertions, 0 failures, 0 errors
  1. About.com
  2. Technology
  3. Ruby
  4. Ruby on Rails
  5. Test-Driven Rails
  6. Running the Test

©2014 About.com. All rights reserved.