You are here: Browse Railsplugins Assert Required
= assert_required Plugin (A Custom Test::Unit Assertion)
A pattern that comes up over and over again is unit testing required fields on models. I created this custom assertion to keep DRY. You pass it the class of the model to test and a hash of the required fields with passing values.
How it works1. Assert you actually have required fields 2. Remove each of the required fields in turn and assert that saving fails each time 3. Assert saving works when all required fields are present
That's all there is to it.
Example def test_required_fields assert_required User, :last_name => ‘Romney’, :first_name => ‘Christian’, :blog => ‘http://www.xml-blog.com’ end API Documentationassert_required accepts three parameters (two required, one optional). The first parameter is the class under test usually something derived from ActiveRecord::Base. The second parameter is a hash (passed to klass.new) which should contain good values for all required fields.The last parameter is an optional message to emit if the assertion fails. The default messages are usually more descriptive, however, so you should think hard before supplying one of your own.
The best documentation, of course, is the source code itself. Have a look, it's only about a dozen lines long.
DependenciesThis plugin makes use of the clean_backtrace method defined in: actionpack/lib/action_controller/assertions.rb
== Feedback
For praise, flames, or bug reports contact me at xmlblog@gmail.com or drop by my website http://www.xml-blog.com
NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly