views:

699

answers:

5

Can someone please clarify using a SIMPLE user story the full slice of what Cucumber would be used for and what RSpec would be used for? I purchased the RSpec book the other day and have been going through it. The author seems to be quite vague at times.

What I'm thinking of if the user story is something like (please excuse the syntax incorrectness, this is just so you get the point):

When a user enters an invalid telephone number then they get a message saying "Invalid Telephone Number"

If I write out all the code for Cucumber to check for this and then write the rspec stuff, I'm basically duplicating my test. Is there a scenario to explain how the cucumber test should be different from the rspec test?

I feel like you would be duplicating tests on both levels all the time.

If there is no definitive answer on this, I'm going to begin to think the Cucumber people just didn't want to step on the RSpec people's toes.

Please help. I feel like my head is about to explode.

Thanks!

+6  A: 

Cucumber is used to explain ( make a description ) of a part (story) of the application rather than unit tests or behaviour tests (which is the focus of RSpec)

So, IMHO Cucumber tests (stories) do not substitute for rspec tests.

The RSpec tests tend to drive development of the models and controllers, and the stories tend to drive development of the views.

By your description seems like you are using cucumber to both test the stories and the behaviour

Diego Dias
I totally agree, this has been my experience as well.
Paul Fedory
+1  A: 

Cucumber can be used to run pretty much any code, which is why I think you're getting confused. But cucumber doesn't provide other kinds of testing facilities like mocking and stubbing methods which make unit testing more specific.

The rspec stuff is really intended to address small bits of behavior and make everything very discrete. If you're familiar with unit testing and the frameworks for that this should make more sense.

The cucumber utility is in being able to translate high-level descriptions to a set of top-level actions on the system.

+3  A: 

Rspec and Cucumber are independent and you can use Cucumber and another test framework for your tests (Test Unit, shoulda etc).

The point is, what do you want to test with cucumber? Because indeed you could end duplicating tests and that would not be really useful isn't it? :)

There are different philosophies with Cucumber.

With Cucumber you can do:

DMA (direct model access meaning, Yes you can fully test your model like you would do in rspec)

Simulated browzer (access entire MVC stack, no javascript)

Automated browser (use webrat and selenium to access your views, with javascript, slower, real browzer)

What I like to do is use cucumber to check what is returned to the user. This is usually what makes sense to me when I define my stories since I don't have really in mind the code I am going to write. So I am testing the final result with Cucumber -> views (using simulated or automated browzer)

Then I use rspec to test any code I write in the controllers and models.

Thus in your case,

When a user enters an invalid telephone number then they get a message saying "Invalid Telephone Number"

I would use Webrat to check that the user get the Invalid Telephone Number message in the view. I would use Rspec to test my controller action and model.

Aurélien Bottazzini
+8  A: 

It might be of use to look into the screencasts at BDDCasts.com. They walk you through creating the stories and specs for an app. It really helped me. Also own the rspec book, but was still confused. You might even want to just check out their source on github.

For me it goes like this:

  • Cucumber to test what the user will see. (Full stack test)

  • Rspec to test everything else. (Model, controller)

coreypurcell
A: 

This article might give you some perspective: When to repeat yourself in tests - and when not to

henning-koch