RSpec vs. Shoulda?
I am new to the unit testing scene; I have only been using unit tests for about 2 months now. When I unit test in Ruby I currently follow the TDD style and use Test::Unit::TestCase. I have also read about RSpec and how it follows the BDD methodology. I also have read about Shoulda which is a in between the two frameworks. My question is, which framework should I devote my time to RSpec or Shoulda, or should I stick with Test::Unit::TestCase? Any opinions on the subject are appreci开发者_运维知识库ated.
Shoulda compliments Test::Unit and RSpec so I don't think it's a case of choosing Shoulda over RSpec, or vice-versa. I use Shoulda on multiple projects, some tested with Test::Unit, others with RSpec, and it works great alongside both frameworks.
I'd recommend trying Shoulda on your existing Test::Unit project. It can save you a lot of key strokes and keep your test classes a bit tidier and more consistent.
As Andrew Vit rightly says, the Shoulda team are favouring the RSpec way of doing things more and more (see http://robots.thoughtbot.com/post/701863189/shoulda-rails3-and-beyond). It's worth trying RSpec and see how you like it. It's not to everyone's taste, but I prefer it to Test::Unit for its syntax, expressiveness, nested contexts, custom matchers and just all round coolness! ;-)
RSpec has a lot of nice features, more than just the syntax too. I would recommend it over Shoulda.
Even Thoughtbot, the authors of Shoulda, are using RSpec now. Shoulda now works as an RSpec plugin and it provides a number of useful matchers.
There's no either-or. Use the different testing tools where they fit best.
Shoulda is a great single-line testing tool. It helps you to keep your tests clean and concise. If you like this style, too, I'd also recommend the Zebra gem. It's a nice little tool to write one-liner tests.
精彩评论