newbie不能在ruby中定义一个方法用于黄瓜测试通过 [英] newbie can't define a method in ruby for cucumber test pass
问题描述
我正在学习黄瓜,下面是一本书的示例代码:
I am trying to learn cucumber, here's an example code from a book:
class Output
def messages
@messages ||= []
end
def puts(message)
messages << message
end
end
def output
@output ||= Output.new
end
Given /^I am not yet playing$/ do
end
When /^I start a new game$/ do
game = Codebreaker::Game.new(output)
game.start
end
Then /^I should see "([^"]*)"$/ do |message|
output.messages.should include(message)
end
当我运行这个规范,我得到这个错误:
When I run this spec, I get this error:
Scenario: start game # features/codebreaker_starts_game.feature:7
Given I am not yet playing # features/step_definitions/codebreaker_steps.rb:15
When I start a new game # features/step_definitions/codebreaker_steps.rb:18
Then I should see "Welcome to Codebreaker!" # features/step_definitions/codebreaker_steps.rb:23
undefined method `messages' for #<RSpec::Matchers::BuiltIn::Output:0xa86a7a4> (NoMethodError)
./features/step_definitions/codebreaker_steps.rb:24:in `/^I should see "([^"]*)"$/'
features/codebreaker_starts_game.feature:10:in `Then I should see "Welcome to Codebreaker!"'
And I should see "Enter guess:" # features/step_definitions/codebreaker_steps.rb:23
看到它提供未定义方法的邮件'
错误,但它在输出
类中定义。
See that it gives undefined method 'messages'
error, yet it is defined in the Output
class.
output.messages.should
与 Output.new.messages.should
,它工作正常。这里有什么问题?
If I replace output.messages.should
with Output.new.messages.should
, it works fine. What is the problem here?
编辑:可能输出
新版本的rails,当我把它改为 outputz
它工作正常。
Edit: Probably output
is a keyword, in new version of rails, when I changed it to outputz
it worked fine. An explanation of this will be accepted as an answer.
推荐答案
显然, 输出
匹配器已添加到版本3.0中的rspec:
Apparently, the output
matcher has been added to rspec in version 3.0:
输出匹配器提供一种方法来断言已将
内容发送到$ stdout
或$ stderr
。
块输出to_stdout或to_stderr。使用
一个字符串,如果块输出特定字符串to_stdout
或to_stderr,则传递。使用regexp或matcher,如果块输出一个
字符串to_stdout或to_stderr匹配,则传递。
With no arg, passes if the block outputs to_stdout or to_stderr. With a string, passes if the blocks outputs that specific string to_stdout or to_stderr. With a regexp or matcher, passes if the blocks outputs a string to_stdout or to_stderr that matches.
p>
Examples:
RSpec.describe "output.to_stdout matcher" do
specify { expect { print('foo') }.to output.to_stdout }
specify { expect { print('foo') }.to output('foo').to_stdout }
specify { expect { print('foo') }.to output(/foo/).to_stdout }
specify { expect { }.to_not output.to_stdout }
specify { expect { print('foo') }.to_not output('bar').to_stdout }
specify { expect { print('foo') }.to_not output(/bar/).to_stdout }
这篇关于newbie不能在ruby中定义一个方法用于黄瓜测试通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!