Don’t be so disgusting, it’s obviously a cucumber.
Or Cucumber with a capital C, perhaps, which was frequently long, hard and red (steady!) until our shiny new testing regime was unveiled!
As part of our commitment to use Cucumber more effectively, here are a few of the things I have learned about its use, and if you learn them too (from me) you will enjoy using Cucumber just as much as this guy enjoys being brutally murdered:
Background, the new GivenScenario
Remember GivenScenario? If you don’t, I don’t blame you. It died in infancy, and in fact was boxed and buried before I even had the chance to use it.
The idea was that you could define some sort of ‘set up’ scenarios that saved you from having the same code repeated all over your feature spec. There were a number of problems with this solution though, as you ended up with scenarios that had no value other than as leg-ups for other scenarios, and it could get pretty confusing if your GivenScenario had a GivenScenario itself, and so many other reasons. It makes you wonder why they ever thought it was a good idea. IDIOTS.
So anyway, you write ugly, repetitive features like this:
Scenario: view preferences page Given I am logged into cas as "Peter" "Portal" with an username of "00700001" And I am a Business School student And I am enrolled on a course And there are campuses And I am on the preferences page Scenario: should show user personal details Given I am logged into cas as "Peter" "Portal" with an username of "00700001" And I am a Business School student And I am enrolled on a course And there are campuses Then I should see "Peter Portal" And I should see "firstname.lastname@example.org"
There is a newer, and better way to do this though. Introducing Background, which you can use in pretty much the exact same way, but it doesn’t count as a scenario itself. Joy.
Background: Given I am logged into cas as "Peter" "Portal" with an username of "00700001" And I am a Business School student And I am enrolled on a course And there are campuses And I am on the preferences page Scenario: view preferences page Then I should see "Your Profile and Preferences" Scenario: should show user personal details Then I should see "Peter Portal" And I should see "email@example.com"
Lovely (this gets much more lovely than you can see here, as there are a lot of scenarios with the same background).
As I was writing some particularly awkward Cucumber steps involving assigning something to something else and then assigning it to something else (see, pretty confusing), and having been told assigning stuff was much harder than it actually turned out to be, I was really struggling to figure out what was going on.
I ended up putting in a load of print statements to see what my variables were doing.
There is a better way though. I was pointed to this article, which describes how you can use
breakpoint in Cucumber steps.
However, you shouldn’t do this, because
breakpoint is deprecated, and it’s now called
debugger. You can put this in anywhere and when you run the test you get an interactive prompt and you can inspect any variable you wish (and probably even set things if you’re trying something out). The most convenient way to do it, as described in the article, is to put it in a step by itself:
Then /^I debug$/ do debugger 0 end
That way you can simply drop in the step whenever you want to know what’s happening:
Scenario: list users Given I am logged into cas as "Peter" "Portal" with an username of "00700001" And I am on the glamlife users index page And I debug Then I should see "Glamlife Users"
When you’re finished with the debugger, just press Ctrl+D, and Cucumber will continue on its merry green way.
You can even put it in a Background and debug all your scenarios at once if you really want to!
Honourable mention goes to
save_and_open_page, which will open the page in a browser so you can see visually what’s wrong with it, instead of having to interpret the HTML, see the Technical Pickles blog for more on this.
As usual there’s a Railscast covering much of this stuff, and more (there’s some particularly good stuff in there for people with many similar features with just a couple of variables), and for anyone who is just here to look at the pictures, there’s also a good one with an introduction to Cucumber.
Don’t be an idiot
All this won’t help you though, like it didn’t help me, if you miss something obvious like the fact that if your ‘visit’ line doesn’t come after your other Givens, you data won’t be there no matter how much you debug. :(