Ruby on Rails 3 - 未定义的局部变量或方法"消息"(NameError)



我正在尝试通过参考"The Pragmatic Programmers - The Rspec Book"来学习Rspec-Cucumber教程。我在第四章,似乎被错误卡住了,无法前进。我完全按照教程进行操作,但是当我尝试在控制台中运行黄瓜功能时,它显示了以下错误(in line : output.messages.should include(message))。

undefined local variable or method `message' for #<Object:0x9c0c05c> (NameError)

我的 codebreaker_steps.rb 文件如下。

Then /^I should see "([^"]*)"$/ do |arg1|
output.messages.should include(message)
end
class Output
    def messages
    @messages ||= []
    end
    def puts(message)
    messages << message
    end
end
    def output
    @output ||= Output.new
    end

如果您花时间熟悉红宝石的基础知识,这将对您非常有帮助。 要回答您的问题,您应该像这样更改步骤定义

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

或者像这样

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

最新更新