What test framework should I use to test my Rails model?

With the many testing framework that is available in the Ruby community namely: unittest, rspec, shoulda, and coulda, what would be the most appropriate testing framework to test my Rails model?

Do they basically have the same functionality, which is to unittest my model? When should I use which? What is the advantage of using one and not the other? Please help me decide.

Thank you very much for your insights.

13.10.2009 22:00:08

I too would suggest starting with Test::Unit just to get a feel for it. Then take a look a look at some of the other frameworks and see what you like best. Shoulda is probably one of the easier ones to get started with coming from plain Test::Unit so it might be an ideal place to look next. On the other hand if you really like the syntax of RSpec you should definitely go with that.

Wars get waged over this topic, but the thing is it doesn't really matter what you use, as long as your doing some form of testing. Also there is nothing wrong with just sticking with Test::Unit if that suits you :)

Personally I'm a fan of the Shoulda + Factory Girl combo.

14.10.2009 06:09:40
Can you please explain why you prefer shoulda over RSpec? I'm interested to know about your decision choosing shoulda. Many thanks.
Joshua Partogi 14.10.2009 06:20:42
Sure :) but there not much to say really. It is just a matter of personal taste from my side. I tried them both and I just like Shoulda's syntax better.
Daniel Kristensen 14.10.2009 08:55:31
Okay. You're right. Somehow shoulda is easier to setup and easier to read.
Joshua Partogi 15.10.2009 13:01:39

The differences mostly come down to personal taste. I recommend starting out with Test::Unit. There's plenty of documentation, and it's the default for Rails. If you want to add Shoulda to the mix later, you can do that very easily.

I also recommend using Factory Girl instead of fixtures. http://github.com/thoughtbot/factory_girl

13.10.2009 22:15:52

Lots of people seem to love rspec. I had a hard time finding recent (and free) resources (tutorials) online on it. There are supposed to be good screencasts, but there's fee to access them. There are free screencasts, but they're very basic. There's a book coming out, but it's been probably a year since it's announced and due out this December. The beta pdf version is ~$50 if you want it early.

I suggest Test::Unit since you don't know it yet. Good to know since you might read someone else's code and need to know about it.

13.10.2009 22:59:39
So Test::Unit is not out of date heh? :-) Ok I will try it out.
Joshua Partogi 13.10.2009 23:27:24
I admit bias in my answer. I'm cheap and haven't had much time/patience to investigate rspec as others have. I WILL look at the rspec book when it comes out.
Jim 13.10.2009 23:31:49