Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Cucumber and when to use RSpec?

I am new to Cucumber testing and I am trying to understand when to use Cucumber and when to use RSpec. For my models, I know I should be testing them with RSpec, and I know that I don't need to write RSpec request specs if I write Cucumber stories instead. The thing I don't know when to use what is with views and controllers. Should I be using RSpec to test my views and controllers, or can I just skip them because I am using Cucumber for my higher level testing? Any advice would be greatly appreciated! Thanks!

like image 367
ab217 Avatar asked Apr 03 '11 21:04

ab217


3 Answers

I use RSpec to drive the development and testing of my models and to some extent my controllers, and Cucumber to drive the development and testing of my views (and subsequently their integration with the controllers).

I don't feel writing Cucumber tests allows me to "skip" writing RSpec tests because a lot of development takes place in the model layer before any views are created.

like image 121
Ant Avatar answered Oct 13 '22 23:10

Ant


If you keep writing Cucumber scenarios instead of unit tests, then you will probably find that your tests become very slow to run. If the tests are slow, you probably won't run them very often, so your development pace will slow down.

You can use RSpec for integration tests operating at the same level as Cucumber. If you need to share and discuss features with non-developers, then Cucumber is a huge help. But even if you're a solo developer, Cucumber helps by acting as a 'mental guard' between the behaviour you need and the underlying implementation.

The standard practice most people follow is to write extensive unit tests for the model, minimal tests for the controller, and very rarely any unit tests for the view. Then you would use Cucumber to check everything is interacting properly (be sure that you scenarios cover any loops/conditions in the controllers and views).

like image 39
Andy Waite Avatar answered Oct 13 '22 21:10

Andy Waite


I'm not sure if it's OK to recommend a book here, but The RSpec Book by David Chelimsky (ISBN 9781934356371) does provide a nice practical example of how Cucumber and RSpec can be interleaved to behaviour-drive the development of a small command-line game in Ruby.

In a nutshell: writing your Cucumber features adopting the point of view of your end-user, and your RSpec specs using your own point of view as a developer is a good balance between execution speed of the test suite, testing granularity (how precise are the error messages when your tests fail) and documentation coverage (the Cucumber features can be seen as an always up-to-date documentation - the very own RSpec documentation is a great example of that).

like image 42
gonzalo-bulnes Avatar answered Oct 13 '22 21:10

gonzalo-bulnes