17

I'm new to unit/func testing in Rails 3. So I'm starting now, better late than never.

I have a method in /lib/mailingjob.rb called find_reply(body)

Right now I'm using cucumber to test this but given that this is all backend, no web interface to this, should I not be using cucumber?

I'm finding when to use RPSEC vs cucumber confusing.

Thanks

2

7 Answers 7

20

In my opinion, you need to use both. Rspec is very good for unit testing, that is testing models, controllers, views. On the other hand, cucumber is a very nice tool to check full scenarios like a user logs in, clicks a link and he is supposed to view this.

I highly advise that you take a look at the cucumber railscast from railscasts.com. Also, make sure that you use webrat and maybe something to auto load your specs like watchr(which i prefer).

3
  • Thanks, I've watched the tutorial and read the RSpec book. But as mentioned, I am stuck with what do I use for what? Given the example I gave above is that for RSpec or cucumber? IDK? Mar 15, 2011 at 17:45
  • In depends on how you test find_reply. If this is just a method of your model that you check individually, use rspec. If on the other hand, you need to check a combination of find_reply, get_reply, send_reply or anything else, better use a scenario with cucumber. In the end, a test is good if it suits your needs, so it can be subjective at times. Some people prefer to make scenarios with rspec as well. But i would stick to unit testing with rspec, scenarios with cucumber.
    – Spyros
    Mar 15, 2011 at 17:55
  • Some alternatives to the tools mentioned here are guard for auto running tests and capybara for integration testing
    – Bnicholas
    Jun 10, 2012 at 5:36
11

I'd advise you to never use Cucumber unless you are specifically employed as a business analyst. That makes your life much easier: all your tests will be in Rspec.

Cucumber takes a toll on your productivity, and your sanity. Running parallel test environments, adding layers of abstraction and breaking your editor with its odd syntax make for lost time. I've written about this extensively on my blog: Why Bother With Cucumber Testing?

2
  • 1
    You'll be happy to hear Spree have moved away from Cucumber. I'm just getting into the BDD frame of mind and I can't help but think I've dodged a productivity-destroying bullet.
    – user419017
    Jan 22, 2013 at 2:10
  • Although this post is old, I must say it is still quite relevant. I just recently got into BDD and was not sure how to be using Rspec or Cucumber and was using them both simultaneously. What a mistake (for me at least). When I perform Rspec tests on my models and controllers, my productivity increases, but when I use Cucumber, it is just a time consumer and I actually feel less productive. I think your response on when to use Cucumber is spot on, as it only seems really applicable for large scale companies, not for little guys like me.
    – Graham S.
    Aug 12, 2015 at 22:05
3

If I didn't already have a Stack Overflow account, I would have made one JUST to write this post. I urge you to re-read @Jack Kinsella's answer

I got in to BDD about 3 months ago, fell in love with it, and quickly started using Cucumber for everything. I forced it in to every project, no matter what

Recently, I've had to learn a yet another language, and my first step was to get the testing environment set up. I came across these 3 articles:

Honestly, their content isn't that important; what's important is that they hinted at an overall idea. I began to suspect that I'd been using Cucumber wrong all along, and I went hunting for answers. This morning I found your SO question, and @Jack Kinsella's blog post


@Jack comes right out and says the idea I believe those articles were tentatively circling. He also gives me the language I was looking for. I now consider his article to be the final word on the subject :)

According to him, what we've ACTUALLY been using Cucumber for is "integration testing". I never understood what that meant before

Unit testing:
How the code works internally. Math.Add(1,1) should be 2, but the website user doesn't care. Just give me a web page!
-> Use RSpec or an equivalent

Integration testing:
How different branches of the code work together to make the website. I type my name and click Log In, and should be taken to the Home page
-> ALSO use RSpec!
(Inside of RSpec, add whatever you need to handle multiple-technologies-touching-eachother. For the code-to-webBrowsers example: Capybara, Watir, etc)

Acceptance testing:
Many of us don't need it. Someone signed a contract with you saying "I'm going to write 'I can add subpages to my micro-site.' in a text file". You write me code that makes it turn green
-> Use Cucumber. Only if you have to do this kind of testing. Which you almost certainly don't

What an elegant solution. No 2nd testing environment. No steps directory and swarm of extra files!

I had a moment of "But I love how Cucumber seperates English from the code". Regular bdd does that too. "rspec --format nested" or Jasmine test results

@Jack is right. Cucumber isn't adding anything; not the way we've been using it. And it's costing a lot. To quote him:

Why not admit to yourself that you don’t do acceptance testing and that you do not need it in your projects? Swap Cucumber for pure integration tests using Capybara, and you’ll be surprised by how much more productive you can be

1
  • I could, of course, be as wrong this time as I was each other time before his article showed me this epiphany. I do hope this is the "final solution", though. It sure seems promising, doesn't it? All 3 concepts divided and explained; "1 framework to rule them all"; elegant solution, I hope!
    – Bukov
    Mar 16, 2013 at 10:16
2

To complete SpyrosP's answer, there is an awesome blog post from Sarah Mei which describes a scenario where you use both Rspec and Cucumber. It is called oustide-in Behavior Driven Development and you can find it here.

1

However, I think most of Jack's problem can be solved by a good cucumber editor. So the problem is not Cucumber. The problem is that there wasn't a capable editor when he wrote this two years ago. But today RubyMine has full cucumber editing capability. While it is not free, it can solve most of Jack's problem: Auto completing, pointing out errors, refactoring. So you probably want to go back and check the points he raised, and see if they are still valid today.

0

My take so this issue many years later is very simple: If you do not know what to use now, just start with RSpec, because it is the central stone for all your testing that you will always need.

With time you will discover that two big testing needs:

  1. Is my code still working?
  2. Can my users still perform the actions already implemented?

As a developer I tend to think that (1) is for every single code change and (2) when I finish a feature and/or deploy.

Having two test suites is nice because during a deployment, you can sit and run a very generic test suite: Are my user features still working? Can I finally deploy this?

Indeed, it should be so generic that if something breaks in (2) you need to do some heavy research (one row missing in your tables) while a fail in (1) should pin-point the exact class with the problem.

Normally I find myself in the situation where I want separate integration+acceptance testing without the Cucumber burden. First I use RSpec tags to filter out these tests. Whenever I need more I like going for Steak+Capybara since it enables Cucumber like objectives on top of something I already know quite well.

0

Definitely use both. Use cucumber for user experience tests(views), use rspec for testing internals(models, etc.). You may write controller rspec tests but I think they are not necessary(try to move the logic to models). Whatever your system is, I strongly advise you to use Cucumber because scenarios will let you and your stakeholders know the functions of your system in a clear written fashion. You will also have your documentation ready and you'll always know where you are and where you are heading. You'll also relate scenarios with support issues in the future. Cuke them right by the way don't use variables in scenarios, they should be understandable. And if you want them to run fast as rspecs use poltergeist(phantomjs).

I've been tempted not use cucumber because in many cases it's not simple as rspec. However every time I understood that I needed scenarios to take full control on the project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.