RSpec vs Cucumber (RSpec stories)

Unit TestingRspecCucumberIntegration TestingRspec Stories

Unit Testing Problem Overview


When should I use specs for Rails application and when Cucumber (former rspec-stories)? I know how both work and actively use specs, of course. But it still feels weird to use Cucumber. My current view on this, is that it's convenient to use Cucumber when you're implementing application for the client and do not understand how the whole system is supposed to work yet.

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?

And, as a corresponding second question: do I have to write specs if I write Cucumber stories? Wouldn't it be double-testing of the same thing?

Unit Testing Solutions


Solution 1 - Unit Testing

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.

Solution 2 - Unit Testing

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.

Solution 3 - Unit Testing

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?

Solution 4 - Unit Testing

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.

Solution 5 - Unit Testing

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!

Solution 6 - Unit Testing

> 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsnitkoView Question on Stackoverflow
Solution 1 - Unit TestingAbieView Answer on Stackoverflow
Solution 2 - Unit TestingJosiah KiehlView Answer on Stackoverflow
Solution 3 - Unit TestingJack KinsellaView Answer on Stackoverflow
Solution 4 - Unit TestingDave GlassborowView Answer on Stackoverflow
Solution 5 - Unit TestingPeppyHeppyView Answer on Stackoverflow
Solution 6 - Unit TestingMarnen Laibow-KoserView Answer on Stackoverflow