2012-08-11 18 views
4

所以我在“实用黄瓜”的第一个项目,我得到了我的步骤定义中未定义的方法错误。错误来自$ ?. success ?.不用说我很困惑。我错过了一个宝石或什么?

这里的步骤定义

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 

这里的错误。

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 

那么,这里有什么问题?我知道.success?是正确的,为什么不是$?注册?谢谢!

回答

10

您需要使用反引号,而不是行情运行命令:

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

应该是:

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

编辑:

只是测试这一点 - 你要非常小心使用这个构造。在黄瓜方案之间,$?的值不会被清除,因此可以很容易地对在先前方案中运行的命令的结果进行断言。您可能希望查看Aruba,这是专门为需要Cucumber执行或断言命令行程序的情况而设计的。

+0

这工作完美(我知道这是愚蠢的)。我一定会去看看阿鲁巴。非常感谢。 – CRBairdUSA 2012-08-13 20:33:32

+0

是的,这个工作很完美。谢谢。 – 2016-01-07 06:54:45

1

代码ruby calc.rb #{@input}是一个Shell命令 - 包括反引号中的命令是告诉Ruby你想要执行一个Shell命令的方法之一。 This link会给你一些关于在Ruby中运行Shell命令的其他方法的信息。

This discussion在实用书架论坛上专门回答您的问题,并且还围绕主题本身进行了一些讨论,如果您想要了解更多信息,它总是很好的。如果您有关于黄瓜书的具体问题,并希望从马的嘴里得到答案,这个论坛可能是您最好的选择。