Ruby Rspec黄瓜:命名法

Ruby Rspec黄瓜:命名法,ruby,rspec,cucumber,Ruby,Rspec,Cucumber,我正在跟踪,我无法理解为什么在运行cucumber时会出现以下错误 Feature: code-breaker starts game As a code-breaker I want to start a game So that I can break the code Scenario: start game # /Users/PC/ruby/practice/rspec/codebreaker/features/cod

我正在跟踪,我无法理解为什么在运行cucumber时会出现以下错误

Feature: code-breaker starts game

  As a code-breaker
  I want to start a game
  So that I can break the code

  Scenario: start game                          # /Users/PC/ruby/practice/rspec/codebreaker/features/codebreaker_starts_game.feature:7
    Given I am not yet playing                  # ruby/practice/rspec/codebreaker/features/step_definitions/codebreaker_steps.rb:17
    When I start a new game                     # ruby/practice/rspec/codebreaker/features/step_definitions/codebreaker_steps.rb:20
    Then I should see "Welcome to Codebreaker!" # ruby/practice/rspec/codebreaker/features/step_definitions/codebreaker_steps.rb:25
      undefined method `messages' for #<RSpec::Matchers::BuiltIn::Output:0x007fd6611fcb30> (NoMethodError)
      ./ruby/practice/rspec/codebreaker/features/step_definitions/codebreaker_steps.rb:26:in `/^I should see "(.*?)"$/'
      ./ruby/practice/rspec/codebreaker/features/codebreaker_starts_game.feature:10:in `Then I should see "Welcome to Codebreaker!"'
    And I should see "Enter guess:"             # ruby/practice/rspec/codebreaker/features/step_definitions/codebreaker_steps.rb:25

Failing Scenarios:
cucumber /Users/PC/ruby/practice/rspec/codebreaker/features/codebreaker_starts_game.feature:7 # Scenario: start game

1 scenario (1 failed)
4 steps (1 failed, 1 skipped, 2 passed)
0m0.050s

shell returned 1
步骤定义文件:


注意:我试过打印output.messages,效果很好。

我相信您遇到了内置的输出匹配器,它是RSpec的一部分,请参阅。是否在尝试检查输出的同一点上尝试打印步骤定义中的output.messages?你也应该受到同样的失败


无论如何,如果您使用不同的方法名称,您应该可以。

我相信您遇到的是内置的输出匹配器,它是RSpec的一部分,请参阅。是否在尝试检查输出的同一点上尝试打印步骤定义中的output.messages?你也应该受到同样的失败


在任何情况下,如果使用不同的方法名称,您都应该可以。

Peter Alfvin是对的:重命名输出方法。以下是对我有效的方法:

class OutputDouble
  def messages
    @messages ||= []
  end
  def puts(message)
    messages << message
  end
end

def output_double
  @output ||= OutputDouble.new
end

Given /^I am not yet playing$/ do
end

When /^I start a new game$/ do
  game = Codebreaker::Game.new(output_double)
  game.start
end

Then /^I should see "([^"]*)"$/ do |message|
  output_double.messages.should include(message)
end

请注意,创建方法输出已重命名为output_double。

Peter Alfvin是正确的:重命名输出方法。以下是对我有效的方法:

class OutputDouble
  def messages
    @messages ||= []
  end
  def puts(message)
    messages << message
  end
end

def output_double
  @output ||= OutputDouble.new
end

Given /^I am not yet playing$/ do
end

When /^I start a new game$/ do
  game = Codebreaker::Game.new(output_double)
  game.start
end

Then /^I should see "([^"]*)"$/ do |message|
  output_double.messages.should include(message)
end
请注意,创建方法输出已重命名为output\u double。

这可能是一个副本,也可能是一个副本