Search code examples
rubyunit-testingtddshoulda

What's the point of some of shoulda's macros?


I think shoulda is really neat, but what I don't understand is why some of the macros exist, such as:

  • should_validate_uniqueness_of :title
  • should_validate_presence_of :body, :message => /wtf/
  • should_validate_presence_of :title
  • should_validate_numericality_of :user_id

I'm relatively new to testing, but what purpose do these serve? They're almost an exact mirror of the same validations that happen in the model. For example, what exactly do you accomplish by going into your model and writing validates_uniqueness_of :title and then writing a test that says should_validate_uniqueness_of :title?


Solution

  • It's for people who want complete defensive test coverage, as in, if someone breaks any function of the app, a test will fail.