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

Testing Validations


Testing Validations in Rails

There is debate whether you should be testing your validations. However, this really falls under the category of "why not?" rather than "why?" While it may seem silly to test validations like validates_presence_of, other validations have subtleties that should be tested to ensure they're working like you intended.

Now that there's something there, go back and take a look at the test/unit directory. It will now contain a file called post_test.rb generated by the scaffold generator. This file is a stub so you can fill in your own tests. It contains a single test called the truth that you can simply delete if you wish. One thing that you'll notice is that tests are not methods as they are using using Test::Unit. Instead, a test method which takes a block works as a replacement for this.

require 'test_helper'

class PostTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
  1. About.com
  2. Technology
  3. Ruby
  4. Ruby on Rails
  5. Test-Driven Rails
  6. Testing Validations

©2014 About.com. All rights reserved.