4

所以我在“Pragmatic Cucumber”中的第一个项目中,我的步骤定义中出现了一个未定义的方法错误。错误来自 $?.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?是正确的,为什么不是$?注册?谢谢!

4

2 回答 2

10

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

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

应该:

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

编辑:

刚刚对此进行了测试-您要非常小心地使用此构造。$?在 Cucumber 场景之间不会清除的值,因此很容易对在前一个场景中运行的命令的结果进行断言。您可能希望研究Aruba,它专为需要 Cucumber 执行或针对命令行程序断言的情况而设计。

于 2012-08-11T18:36:21.750 回答
1

代码ruby calc.rb #{@input}是一个 Shell 命令——包括反引号中的命令是告诉 Ruby 你想要执行一个 Shell 命令的方法之一。此链接将为您提供有关在 Ruby 中运行 Shell 命令的其他方法的一些信息。

Pragmatic Bookshelf 论坛上的此讨论专门回答了您的问题,并且围绕主题本身进行了一些讨论,如果您想了解更多信息,这总是好的。如果您对 Cucumber 书有特别的疑问,并想从马嘴里得到答案,那么这个论坛可能是您最好的选择。

于 2013-11-21T16:20:45.537 回答