以下はThe Rspec Bookからのものです。なぜmessages << message
indef puts
が でないのか不思議です@messages << message
。それ||= []
がメモ化であることは理解していますし、それ@messages
がインスタンス変数であることも理解しています。では、同じクラスで使用する必要がありますか?それともタイプミスですか?
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