views:

4964

answers:

6

I am setting up a rails app and I just finished making some unit tests and my friend said that apparently fixtures are no longer cool and people are now using RSpec or shoulda. I was wondering what the actual benefits are to use these other toolkits. Any information at all is appreciated.

-fREW

+2  A: 

RSpec is way more powerful because it's far easier to both read and write tests in. It's also very elegant when using mocks and stubs, a concept which will become extremely useful once you start using them in your tests. Try it in a simple test app (NON RAILS!) and you'll see how elegant your specs are versus the equivalent standard testing.

Loren Segal
+11  A: 

RSpec and similar framewroks are tooling designed to aid in Behavior Driven Development. They're not just a prettier way to write tests, though they do help with that.

There is plenty of information on BDD here: http://behaviour-driven.org/ And wikipedia: http://en.wikipedia.org/wiki/Behavior_Driven_Development

There are too many benefits to list here, so I'd recommend browsing that site a little.

aaronjensen
+12  A: 

I personally prefer Shoulda to RSpec. I find that Shoulda has less magic syntax than RSpec. My problem with RSpec is that yeah it's very readable when I read it aloud, but when I get to writing it, hmmmm, I'm never sure how a given assertion should be written. Prag Dave explains the problem better than me. He also likes Shoulda and has a few examples.

webmat
I totally agree webmat. I really struggle with understanding RSpec syntax because it has so much syntactic sugar. It turns ordinary phrases into code but then you have figure out what it is supposed to do! The curve is steep and I am lazy.
srboisvert
+4  A: 

There are two different things here:

The first thing is what framework to use for writing tests/specs. Here you can choose between Test::Unit, RSpec, Shoulda and so on. The choice is whether you want to do traditional TDD (Test::Unit) or whether you prefer the alternative ways of thinking about specifiying behaviour advocated by developers like David Chemlinsky (RSpec and to some extent Shoulda).

The second thing is how to handle test data. There are Rails fixtures and alternatives desgined with other goals such as the FixtureReplacement plugin. Before Rails 2.0 fixtures had significant and well-documented pratical problems. Many of the practical issues were fixed in Rails 2.0. However fixtures can lead to inadvertent test coupling and some of the alternatives try to avoid this.

domgblackwell
+2  A: 

Check out Josh Susser's The Great Test Framework Dance-off for a comparison of the popular Ruby testing frameworks.

Mike Breen
A: 

What’s the best Rails testing framework? The one that makes your testing life most enjoyable, of course. For me, this has been RSpec, but I’m beginning to prefer Shoulda.

Shoulda provides an elegant DSL for doing Behavior-Driven Development in Test::Unit. In addition, Shoulda includes macros for many common tasks in unit and functional tests. Thoughtbot, the company behind Shoulda, has created an excellent tutorial. I also recommend Tammer Saleh’s presentation on BDD with Shoulda. SPAM

please do not post ads in the comment
Deepak N