Topic: What's the point of some of the shoulda matchers?

Given this example:

describe Post do
  it { should belong_to(:user) }
  it { should validate_presence_of(:title) }
end

Why are these tests even necessary when they are exact mirrors of the rails functions? Isn't that essentially testing the framework? This kind of thing seems pointless to me. If I have a method call in my Post class that says "belongs_to(:user)", why would I bother testing that?

Last edited by ryeguy (2011-02-04 15:24:55)

Re: What's the point of some of the shoulda matchers?

If you follow the idea of writing tests first then writing the code then they make more sense: you would write your shoulda tests as a specification for what the model _should_ do, and then implement the corresponding code in your model to make it so.

Testing the framework would get into the nitty gritty of how the belongs to association works (should find with foreign key, etc, etc) or how you validate presence (should have error if blank, etc, etc) and this would add a load of tests.

Although I agree sometimes the distinction between testing your code vs the framework does get blurry smile

Rob Anderton
TheWebFellas

Re: What's the point of some of the shoulda matchers?

Also tests is a way of communicating between developers. Adding (or not adding) certain tests you can point others to something... Also, adding tests for validations and associations protects you from breaking things accidently.