Try something like
class TestModel < Test::Unit::TestCase def setup @model_instance = Model.new end
## add here whatever you can think as empty value def test_should_not_body_be_empty assert_error_on @model_instance, :body, :body => nil assert_error_on @model_instance, :body, :body => "" end
# TODO: clean up this to use the first key of values hash as field def assert_error_on(model, field, values = {}) # there is no assert here cuz others field may have errors # and the idea is test one thing at time. model.valid? assert not model.errors[field].nil? end
# TODO: clean up this to use the first key of values hash as field def assert_no_errors_on(model, field, values = {}) # there is no assert here cuz others field may have errors # and the idea is test one thing at time. model.valid? assert model.errors[field].nil? end end
Try to keep your tests clean, do one thing at time, and reflect that thing in the test name. And add as many test you need. Always remember that Test Cases are classes so add helper method to avoid repetitive tasks is not only good, its a must.
With this idea in mind test something like this:
class Model < ActiveRecord::Base validates_presence_of :name validates_length_of :name, :in => 4..128 validates_uniqueness_of :name end
will take at least one test per validation, but you probably wrote the
tests first right?