Simple Ruby on Rails Unit Tests

Posted by Scott Wed, 09 May 2007 22:43:00 GMT

Some Rails programmers skip all unit tests that they consider simply “testing Rails”. I agree that you should not waste time writing unit tests to test that “validates_uniqueness” is working. It is.

Yet, I do like to start off creation of a new model with a simple test to ensure that I wired up everything correctly. Is the database table created as I expect? Did I wire up the right field names in the fixture? Did I have any typo in the model name? Did I accidently create the model in plural format (e.g. referral_visits instead of referral_visit)?

So, I create a simple test that some would consider is just testing Rails. I do it to test me!

Here’s one I wrote just now, for example.


  def test_save
    referral_visit = ReferralVisit.new(:account_id => /
      referral_visits(:visit1_from_synap_link).account_id)
    assert referral_visit.save!
  end

A simple test like this gives you comfort that all is set up like you expected and it also helps get the test case juices flowing to encourage you to continue with the practice of test-driven development.

Comments

(leave url/email »)