Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newbie Cucumber hang-up $?.success? no method error

So I'm on the first project in "Pragmatic Cucumber" and I'm getting an undefined method error in my step definitions. The error is coming from $?.success?. Needless to say I'm confused. Am I missing a gem or something?

Here's the Step Definition

Given /^the input "(.*?)"$/ do |input|
  @input = input
end

When /^the calculator is run$/ do
  @output = 'ruby calc.rb #{@input}'
  raise('Command failed!') unless $?.success? #$?.success? is failing. look that up.
end

Then /^the output should be "(.*?)"$/ do |arg1|
  pending # express the regexp above with the code you wish you had
end

Here's the Error.

Feature: Adding

  Scenario: Add two numbers       # features/adding.feature:3
Given the input "2+2"         # features/step_definitions/calculator_steps.rb:1
When the calculator is run    # features/step_definitions/calculator_steps.rb:5
  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'
Then the output should be "4" # features/step_definitions/calculator_steps.rb:10

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

1 scenario (1 failed)
3 steps (1 failed, 1 skipped, 1 passed)
0m0.012s

So, what's the problem here? I know that .success? is correct, why isn't $? registering? Thanks!

like image 775
CRBairdUSA Avatar asked Dec 21 '22 17:12

CRBairdUSA


2 Answers

You need to use backticks instead of quotes to run your command:

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

Should be:

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

Edit:

Just tested this - you want to be very careful using this construct. The value of $? won't be cleared out between Cucumber scenarios, so it would be easy to make an assertion against the result of a command that was run in a previous scenario. You may wish to look into Aruba which is specifically designed for situations where you need Cucumber to execute or assert against command-line programs.

like image 158
Jon M Avatar answered Jan 01 '23 03:01

Jon M


The code ruby calc.rb #{@input} is a Shell command - including the command in backticks is one of the ways to tell Ruby that you want to execute a Shell command. This link will give you some information on other ways to run Shell commands in Ruby.

This discussion on the Pragmatic Bookshelf forums specifically answers your question, and also has some discussion around the topic itself, which is always good if you want to learn more. If you have questions specifically about the Cucumber book and want to get your answers from the horse's mouth, this forum is probably your best bet.

like image 27
aspen100 Avatar answered Jan 01 '23 04:01

aspen100