我正在跟踪“Rspec手册”,我无法理解为什么在运行黄瓜时会出现以下错误。
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步骤定义文件:
http://pastebin.com/BZZKL0wa
注意:我试着打印output.messages,它运行得很好。
发布于 2015-01-25 23:04:19
我相信您正在遇到内置的output matcher,它是RSpec的一部分(参见https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/output-matcher)。在尝试检查输出的同时,您是否尝试在步骤定义中打印output.messages?你应该得到同样的失败。
无论如何,如果使用不同的方法名,则应该没有问题。
发布于 2016-02-01 20:03:47
Peter Alfvin是对的:重命名output方法。以下是对我起作用的东西:
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。
https://stackoverflow.com/questions/28142579
复制相似问题