Cucumber and RSpec: A Beginner’s First Impression

by Matt Montaruli on September 20, 2012

We’ve recently migrated our testing framework for a Rails app we’re building over to RSpec and Cucumber.

I’ve been following the BDD process with these tools for about a week now and am brand new to both BDD and RSpec/Cucumber. I’ve found that I quite enjoy working with these tools and have learned a lot about testing and Rails in general during this learning process.

Here are some of my early experiences with this testing framework.

Disclaimer: I’m definitely new to BDD (and relatively new to Rails for that matter), so my below observations may very well be incorrect.

Needs Some Getting Used To

It’s been taking some time to learn the framework, language and the “Test”, “Code”, “Refactor” BDD cycle, however as I work with these tools more and more, I find I am starting to get the hang of it and enjoy the test first, code second process.

“Features” are Higher-Level, “Specs” are Lower-Level

I like how Cucumber’s “Features” express what you are looking to accomplish at a high-level. Write these without getting distracted with the minutia of how it will get done. This allows you to focus on the behavior of the application, rather than how a specific line of code functions.

Features are easily readable and allow for flexibility if something within the application changes–you may have to change a step definition or two, but unless you are changing a feature of the application, the Cucumber Feature will likely stay the same.

RSpec and Cucumber complement each other well. With RSpec, you test the actual detail of how a feature will operate. It’s great for lower-level, more detailed testing.

Together, the two seem to create focus in both what the application should do and how it should do it. The way these two tools work together make them very enjoyable to work with.

It Facilitates Writing Leaner Code

It seems the logical way to code is to write the code first, then to test it. However, by writing the tests first and then just enough code to make the test pass, this helps to hone in on the bare minimum of what needs to be accomplished and keeps the code as lean as possible.

Seems Like a Lot More Initial Setup

My first tests seem to take a lot more time to write. This could be because there is a learning curve and I get better as I work with the framework. However, it also appears that the framework builds on itself as you write more tests.

For example, since you can reuse step definitions, testing features gets easier and easier to write. I’ve noticed that as I move along and write more tests, the entire process goes faster and faster.

More Tests to Ensure Proper Functionality

Since every feature and function must be have a test written for it first, this results in writing a lot more test code than I’m used to. This may be a good thing because it will make it easier to remove bugs from my software.

Also, I’ve found “headless testing” (that is, testing without the browser) to be a much more efficient way to test. It is much quicker than going through your application in the browser and testing for bugs that way.

Care to share any of your own BDD and/or RSpec/Cucumber experiences? Let us know in the comments below.