Better Testing with RSpec

April 6, 2015
Better Testing with RSpec

Let’s get this one question out of the way, right off the bat. Why RSpec? or more specifically - why RSpec instead of MiniTest?

For me, the answer was provided with a simple empirical test. One of my junior developers desperately wanted us to use RSpec on our project instead of Test::Unit (precursor of MiniTest). We decided it to give it a trial. I don’t think we wrote tests faster. Or that the specs ran faster. But… my fellow developers on this project wrote better tests by far than they had been writing using Test::Unit.

Better tests in this case mean… more finely grained, better focused. Where before we wrote a single test with 15 or more asserts, now we wrote 8 or 10 specs, most with single expectations.

While I am certain that it was possible for these developers to write better, more finely grained tests using Test::Unit and a little discipline, I am also convinced that we wrote better tests by default in RSpec, because of the way in which the specs are structured, and described.

So ever since, I have advocated for RSpec on every project I work on.

Now, it does take a little longer to get up to speed using RSpec, but once you have the basics down, the rich environment of the RSpec core, combined with separate libraries for expectations & mocking provide both power and flexibility. If you don’t like rspec-mock, you can go ahead and substitute the mocking/stubbing framework of your choice. And rspec-rails provides a drop-in replacement for the default Rails testing framework.

The RoR4Real Advanced Topics workshop - Better Testing with RSpec - will help you over that speed-bump of getting started using RSpec, and introduce you to the full power of the RSpec environment covering:

  • RSpec basics

    • Setting up your project with RSpec
    • describe & context blocks
    • expectations in detail
    • using test doubles
    • rspec-rails intro
  • More advanced concepts

    • writing your own matchers
    • the power of factories
    • integration testing with Capybara

So, to get back to the original question, “Why RSpec”, let’s see what one of the authors of The RSpec Book had to say:

RSpec is not just about RSpec. It’s about BDD. It’s about encouraging conversation about testing and looking at it in different ways. It’s about illuminating the design, specification, collaboration and documentation aspects of tests, and thinking of them as executable examples of behaviour. – David Chelimsky

And that’s why I use RSpec. If you’d like to write better tests in RSpec like a pro, you should sign up (and get 50% off!!) for one of my Advanced Rails Workshops now:

Signup Now!

Even if you don’t sign up now, if you are intrigued by RSpec, please do vote for this topic in my Advanced Rails Workshops Topics Survey.

} else { $('#cookie-notice').show(); } $('#cookie-notice-accept').click(function() { createCookie('cookie-notice-dismissed','true',365); // Change from 1 month to 1 year. $('#cookie-notice').hide(); });