Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between RSpec and Cucumber? [closed]

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.

like image 510
banditKing Avatar asked Aug 01 '12 15:08

banditKing


People also ask

What is RSpec in Cucumber?

RSpec. Cucumber. 1. A testing framework which gives the option to build and execute tests. A tool which is used to create test cases in plain English text.

Is RSpec TDD or BDD?

RSpec is a Behavior-Driven Development tool for Ruby programmers. BDD is an approach to software development that combines Test-Driven Development, Domain Driven Design and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD.

What is RSpec used for?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.

What is RSpec and capybara?

The tools we'll be using. For this exercise we'll be using RSpec, anecdotally the most popular of the Rails testing frameworks, and Capybara, a library that enables browser interaction (clicking buttons, filling out forms, etc.) using Ruby. You may find some of the RSpec or Capybara syntax confusing or indecipherable.


1 Answers

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.

like image 184
DVG Avatar answered Sep 30 '22 00:09

DVG