Spree Commerce

Try It Now

Goodbye Test::Unit (You won't be missed)

Posted on March 26, 2008 by Sean Schofield

I’ve never been a big fan of the testing that ships with Rails. In particular I have been struggling with a way to properly test my model objects without dealing with fixture data or any dependency on the database. In the case of ActiveRecord that is a little bit unrealistic since it needs access to the database to provide the field name accessors and other goodies. So I needed a solution that would help me mock the stuff ActiveRecord provides without having to manually specify it in the mock (otherwise that kind of defeats the point of using ActiveRecord right?)

Enter the rspec on rails plugin and the very cool mock_model helper. With this simple helper you get a mock of your ActiveRecord object with minimal database overhead. I’ve also decided to bypass fixtures completely since they aren’t really appropriate for a unit test IMO. The other major reason is that they add a ton of time (several seconds) to the test execution. Unit tests are crucial to TDD/BDD and if you’re running them dozens/hundreds of times a day these sorts of annoyances add up.

To run the tests from the spree code just type the following

<p>cd starter-app<br />
rake spec</p>

Fixtures are another testing annoyance that we are phasing out. Some instances have crept into the code through the contributions of other developers back when the project was starting. Our new approach does not rely on fixtures for the unit tests. I do find fixtures useful in other contexts but that is the subject of another blog post.

It looks like we will be completely phasing out the Test::Unit stuff shortly.