What's the difference between RSpec and Cucumber?

banditKing picture banditKing · Aug 1, 2012 · Viewed 32.5k times · Source

I have 6 months of Rails development experience. I've built a web application that's in use now with authentication and authorization and postgresql db.

I'm moving on to my second Rails application but this time, after lessons learnt, I would like to develop it using TDD, since I noticed its a lot easier to scale it and fix bugs. It's slow to develop but in the long run its much easier to deal with.

I have heard of Rspec and Cucumber but am thoroughly confused by them.

I would like to know what the difference is between RSpec and Cucumber and what they are used for.

It would also be useful to know if, from the perspective of a beginner (who is also the sole developer) whether a testing framework is really needed.

Answer

DVG picture DVG · Aug 1, 2012

RSpec and Cucumber are both testing frameworks. RSpec includes traditional Unit Testing (which means testing a class or part of the application in isolation from the rest of the application. So your model does what your model is supposed to do, the controller does what it is supposed to do, etc).

RSpec and Cucumber both are used for Acceptance Testing (Which is called ATDD, BDD, Specification by Example, etc depending on who you ask). These are business-case driven Integration Tests, which mean they simulate the way a user uses the application and uses the full Rails stack so problems with the way the different parts of your application work together can be found in a way that unit testing will not find.

The main difference between RSpec and Cucumber are the business readability factor. Cucumber's main draw is that the specification (features) are separate from the test code, so your product owners can provide or review the specification without having to dig through code. These are the .feature files that you make in Cucumber. RSpec has a similar mechanism, but instead you describe a step with a Describe, Context or It block that contains the business specification, and then immediately have the code that executes that statement. This approach is a little easier for developers to work with but a little harder for non-technical folks.

Which to use? If you are the sole developer and product owner, then I would stick with RSpec, I feel it's easier to a technical person to understand, offers a few advantages in keeping things scoped and under control, and keep you out of messing with RegExs for test steps. If you are building this for a client, and they are hands-on with regard to the Specification, go with Cucumber for your Acceptance Test and use RSpec for Unit Tests.

Just to demonstrate the main difference between the two:

Cucumber:

#articles.feature
Given an article exists called "Testing Demonstration"
When I visit the list of articles
Then I should see an article called "Testing Demonstration"

#article_steps.rb
Given /^an article exists called "(.+)"$/ do |title|
  FactoryGirl.create(:article, title: title)
end 
When /^I visit the list of articles$/ do
  visit articles_path
end
Then /^I should see an article called "(.+)"$/ do |title|
  page.should have_content title
end

Rspec

describe "Articles" do
  let(:article) { FactoryGirl.create(:article) }
  context "Index Page" do
    before { visit articles_path }
    it { page.should have_content article.title }
  end
end

This blog series is excellent on getting going with RSpec.