small medium large xlarge

06 Feb 2012, 06:45
Krishna Aradhi (2 posts)

I am going through the PDF version of the P1.0 release of ‘The Cucumber Book’ and am stuck on the ‘Adding.feature’ example in Chapter 2.

This is the output I get when I run @cucumber –format progress@ after filling in the @When /^the calculator is run$/@ step definition (Page 17).


(::) failed steps (::)

undefined method `success?' for nil:NilClass (NoMethodError)
./features/step_definitions/calculator_steps.rb:7:in `/^the calculator is run$/'
features/adding.feature:5:in `When the calculator is run'

Failing Scenarios:
cucumber features/adding.feature:3 # Scenario: Add two numbers

1 scenario (1 failed)
3 steps (1 failed, 1 skipped, 1 passed)

Anyone knows how to fix this?

06 Feb 2012, 17:49
Matt Wynne (87 posts)

Hi Krishna,

It sounds like you’ve made a mistake typing the code in from the example. Can you paste the complete contents of features/step_definitions/calculator_steps.rb into, then send us the link, please?

07 Feb 2012, 08:58
Krishna Aradhi (2 posts)

EDIT: My bad; I should have noticed that it is a shell command being executed on line 6, hence the use of those backticks there. The code works just fine now.

Hey Matt,

Here are the contents of the file you asked for:

The only (silly) problem seems to be with the single quotes I am using on line 6. I downloaded the source code for ‘The Cucumber Book’ from its web page and copying code from there into my @calculator_steps.rb@ file works just fine. When I type in the code myself, however, the error I mentioned above gets thrown.

Here is the line of code I am talking about:

@output = 'ruby calc.rb #{@input}'

It seems you are using backticks in the source code instead of single quotes. Is that how it is supposed to be while using Cucmber?

08 Feb 2012, 18:01
Matt Wynne (87 posts)

Glad to hear you got it working!

23 Mar 2012, 01:33
Jose E (10 posts)

Hello Krishna and Matt,

I’m sad to report that I have the same problem here. and it’s really weird because I have the same code listing as Krishna, and I figured before even attempting to run it, that I had to use quotes or double quotes, but in spite of that it’s still not working :( Here’s the code:

Can you help me out?

23 Mar 2012, 07:56
Matt Wynne (87 posts)

Hi Jose,

Did you realise you need to wrap the command with backticks? That’s the @`@ character. This tells Ruby to run it as a shell command.

23 Mar 2012, 14:37
Jose E (10 posts)

Oops, my bad, I had no idea ! I thought what was said just here was that it had to be done with double or single quotes, instead of backticks, so sorry, anyways, I got it working, thanks, Btw, it’s a pain having to type a backtick, where do I find it in an EN keyboard?

23 Mar 2012, 15:10
Matt Wynne (87 posts)

Great, glad you got it working.

bq. where do I find it in an EN keyboard?

Mine is just to the left of the Z key.

If you get stuck like this again, it’s best to download the code samples from the website and try those - if they work then you know you must have made a typo!

24 Mar 2012, 16:18
Jose E (10 posts)

Thank you Matt ! thank you very very much! One question off topic, Do you know where can I find solutions to the “Try this” exercises ? I’m struggling with some simple ones like the ones in Ch. 4 page 48.

24 Mar 2012, 16:30
Jose E (10 posts)

For example, I have a .feature file like this:

Feature: Display of airline departures Scenario: Verify that flight codes are written correctly Given the flight EZY4567 is leaving today

and a .rb file like this:

Given /^the flight ([A-Z]*)(\d+) is leaving today$/ do |flight_code| end

And I have the following problem(s):

Given the flight EZY4567 is leaving today # features/step_definitions/departures_steps.rb:1 Your block takes 1 argument, but the Regexp matched 2 arguments. (Cucumber::ArityMismatchError) features/step_definitions/departures_steps.rb:1:in /^the flight ([A-Z]*)(\d+) is leaving today$/' features/departures.feature:3:in Given the flight EZY4567 is leaving today’

Failing Scenarios: cucumber features/departures.feature:2

Can you help me build the right regex for these exercises?

Thank you !

24 Mar 2012, 16:43
Jose E (10 posts)

[UPDATE] and Question,

Something really interesting happens with this code:

@Given /^the flight [A-Z]+(\d+) is leaving today$/ do |flight_code| puts “code: #{flight_code}” end@

This snippet seems to work well for all the cases required by the exercise, But I AM COMPLETELY puzzled about the puts line printing an Incomplete code like this:

*Scenario: Verify that flight codes are written correctly features/departures.feature:2 Given the flight EZY4567 is leaving today features/step_definitions/departures_steps.rb:1 code: 4567 Given the flight C038 is leaving today features/step_definitions/departures_steps.rb:1 code: 038 Given the flight BA01618 is leaving today features/step_definitions/departures_steps.rb:1 code: 01618 *

1 scenario (1 passed) 3 steps (3 passed) 0m0.002s

Why aren’t the letters printed out?

Thanks in advance!

24 Mar 2012, 21:14
Matt Wynne (87 posts)

Hi Jose,

The Try This exercises aren’t meant to have solutions - it’s up to you to find them for yourself!

It’s hard to read your output, but it seems that you’re just seeing the numbers rather than the letters and numbers of the flight code. Take another look at how capture groups work. You need to use paratheses around everything you want to capture…

25 Mar 2012, 01:50
Jose E (10 posts)

Matt, you’re right on the fact that I’m only capturing numbers, I’ve been playing with the Regex for the steps for a while, and I came to the conclusion you’ve just given to me, I need to group stuff with parenthesis. At first I found it weird since pure Regex in ruby doesn’t require this. Anyways, It seems I’m now well on the way to learning Cucumber!

Thank you Mr. Wynne

03 Oct 2012, 16:19
ldavis (1 post)

Thanks all for the posts!! Very helpful as I’m doing this on a Windows machine and have been stumped for a few minutes by the back ticks vs single quotes.

  You must be logged in to comment