Difference between Rspec and Cucumber

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 is a popular framework for unit testing and Cucumber is used for integration testing and behavior driven development.Unit tests with rspec confirm that small, discrete portion continue working as developers add features.

Integration tests built with cucumber determine wether the application’s features work as expected,testing the application from user point of view.

Rspec is very good for unit testing, that is testing models, controllers, views. On the other hand, cucumber is a very nice tool to check full scenarios like a user logs in, clicks a link and he is supposed to view this.

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.

Let me give an example

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

Thanks for visit…

Leave a comment