我跟随'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并且工作正常。
答案 0 :(得分:3)
我相信你已经遇到了内置的output
匹配器(RS https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/output-matcher)。您是否尝试在步骤定义中打印output.messages
,同时尝试检查输出?你应该得到同样的失败。
无论如何,如果你使用不同的方法名称,你应该没问题。
答案 1 :(得分:2)
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。