Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec vs Cucumber (RSpec stories) [closed]

People also ask

What is the difference between RSpec and cucumber?

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.

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.


If you haven't already, you might want to check out Dan North's excellent article, What's in a Story? as a starting point.

We have two main uses for Cucumber stories. First, because the story form is very specific it helps focus the product owner's articulation of the features he wants built. This is the "token for a conversation" use of stories, and would be valuable whether or not we implemented the stories in code. Second, when the process is working well enough that we have complete stories before we begin writing the feature (more of an ideal that we strive for than a daily reality), you have your acceptance criteria spelled out clearly and you know exactly what and how much to build.

In our Rails work, Cucumber stories do not substitute for rspec unit tests. The two go hand in hand. In practice, the unit tests tend to drive development of the models and controllers, and the stories tend to drive development of the views (we tend not to write rspec for our views) and provide a good test of the application as a whole from the user's perspective.

If you're working solo, the communication aspect may not be that interesting to you, but the integration testing you get from Cucumber might be. If you take advantage of webrat, writing Cucumber can be fast and painless for a lot of your basic functionality.


Think of it as a cycle:

Write your Cucumber feature, then while developing the pieces for that feature, write specs to complete the individual components. Continue completing specs until you've written enough functionality for the feature to pass, then write your next feature.


My take is that it's a bad idea to use Cucumber in most situations due to the costs in productivity its syntax incurs on you. I wrote extensively on the topic in Why Bother With Cucumber Tests?


A Cucumber story is more a description of the overall problem your application is solving, rather than if individual bits of code work (i.e. unit tests).

As Abie describes, it's almost a list of requirements that the application should meet, and is very helpful for communication with your client, as well as being directly testable.


Nowadays you can use rspec with Capybara and Selenium Webdriver and avoid having to build and maintain all of the Cucumber story parsers. Here is what I would recommend:

  1. Write out your story
  2. Using RSpec, I would create an integration test ex: spec/integrations/socks_rspec.rb
  3. Then I would create an integration test which includes a new describe and it block for each scenario
  4. Then I would implement the minimal functionality require to get the integration test and while going deeper back (into controllers and models, etc) I would TDD on controllers and models.
  5. As you come back up your integration test should pass and you can continue to add steps to the integration test
  6. repeat

One thing to note, however, is that the controller and integration tests have overlap that may not be necessary so you have to use your best judgement so you do not waste your time.

Also, once you find your groove you will find it most enjoyable to develop using BDD, until then don't feel guilty if you don't feel like you are doing it perfect and don't over think it. You will do great!


But what if I'm doing my own project? For most of the time, I know how the parts of the system interact. All I need to do is to write a bunch of unit-tests. What are the possible situations when I would need Cucumber then?

You still need Cucumber. You need it to document how you see the system working, and you need it to make sure you haven't broken functionality when you change things.

In other words, you need Cucumber stories for the same reasons as you need unit tests -- they just work on a higher level of abstraction.