Rspec黄瓜:NoMethodError
Rspec Cucumber: NoMethodError
我正在关注 'The Rspec Book',但我无法理解为什么在 运行 黄瓜时出现以下错误。
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 并且效果很好。
我相信您已经 运行 进入了内置 output
匹配器,它是 RSpec 的一部分(参见 https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/output-matcher)。您是否在尝试检查输出的同时尝试在步骤定义中打印 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。
我正在关注 'The Rspec Book',但我无法理解为什么在 运行 黄瓜时出现以下错误。
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 并且效果很好。
我相信您已经 运行 进入了内置 output
匹配器,它是 RSpec 的一部分(参见 https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/output-matcher)。您是否在尝试检查输出的同时尝试在步骤定义中打印 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。