Appropriately unit testing my models?

Matthew Williams wrote:

Can anyone point me towards a good resource on appropriately testing all the fields in a given model? Or perhaps give some advice.

For example, this test below is for a model with only a single column, but when I add a new column (a date field for example), how would I extend the test to cover both the body and date column of the model?

  def test_that_status_note_body_is_valid     note = Note.new     # Empty body field     assert !note.valid?     assert note.errors.invalid?(:body)     note.body = nil     # Nil body field     assert !note.valid?     assert note.errors.invalid?(:body)     note.body = "Testing body that should pass"     # Correct body field     assert note.valid?     assert !note.errors.invalid?(:body)   end

Would I create a new test called "test_that_date_is_valid" and only test against that or would I create tests that build instances of "Note" that contain as many test cases as it would take to test against every case? (which seems correct but when I think about how many tests would be involved for a model with even 5 columns it then seems to look not so correct)

I appreciate the responses, thanks!

Maybe you're can use a generic new method - something like:

def test_status_for_something    note = new_note(:body => nil)    ... end

def new_note(options)    Note.new({:body => "Default body", :header => "Default header", :date => Date.today}.merge(options)) end

Btw, I suggest that you split your testcases into smaller pieces, the above could be split into test_body_should_be_invalid_when_nil, test_body_should_be_invalid_when_empty_string, test_body_should_be_valid_when_nonempty_string.

Makes it easier to spot the problem when a test breaks.