Tuesday, May 01, 2007

Ruby: Validatable Tests

In the comments for my most recent entry, Validatable 1.2.2 released, Jon Leighton asks: how do you recommend testing validations?

I've had this discussion before when I wrote about testing ActiveRecord validations. I've not been able to come up with a 'best practice', but I've had some fun trying out different solutions.

My latest attempt at a solution alternative is to create assertions for the framework. For example, if I were to create a Validatable framework, that framework would include ValidatableAssertions.

To create validations for the Validatable framework I wrote my ideal syntax and then figured out how to make it work. The following code was the original version for validating that a class contained a presence of validation.
class FooTest < Test::Unit::TestCase
Foo.must_validate.presence_of :name
end
I originally chose the above syntax because it was expressive enough to convey my intent in the code. I spent a bit of time making this syntax work, but in the end I went with a syntax that I found a bit drier, and easier to implement.
class FooTest < Test::Unit::TestCase
include ValidatableAssertions

Foo.must_validate do
presence_of :name
format_of(:name).with(/^[A-Z]/)
numericality_of(:age).only_integer(true)
end
end
The above code creates a test for each line in the block given to must_validate. If the Foo class does not contain a presence of validation for name, an error with the text "Foo does not contain a Validatable::ValidatesPresenceOf for name" will be raised.

Clearly this solution has limitations. Any validates_true_for validation cannot be tested using this DSL style of testing. Furthermore, any validation that uses an :if argument cannot use this DSL, since those validations require an instance to eval the :if argument against. However, for validations that are not validates_true_for and do not rely on an :if argument, the ValidatableAssertions can replace various existing success and failure validation tests.

If you hate this idea, no worries, you can always test your classes the traditional way: creating an instance and calling the valid? method. This DSL is another tool that may or may not help you out based on the context in which you use it.

The ValidatableAssertions are available in gem version 1.3.0 and greater.

4 comments:

  1. Anonymous11:22 PM

    When do you plan to release this? I might like to use this in my TDD tutorial at RailsConf. If you don't release by then I'll just grab and build from trunk, but it would be cool if it was out and about.

    ReplyDelete
  2. Anonymous6:25 AM

    David,

    Thanks for the interest. I released a new gem (1.3.0) this morning.

    Cheers, Jay

    ReplyDelete
  3. Anonymous3:40 AM

    How would you use Validatable with a web form (i.e. in rails)? Or is it better to do a hack on ActiveRecord as described here:

    http://wiki.rubyonrails.com/rails/pages/HowToUseValidationsWithoutExtendingActiveRecord

    ReplyDelete
  4. Anonymous9:26 PM

    Most often people back their forms with Presenters if they want to take advantage of Validatable.

    ReplyDelete

Note: Only a member of this blog may post a comment.