我正在阅读 RSpec Book,我有以下测试代码:
require 'spec_helper'
module Codebreaker
describe Game do
describe "#start" do
let(:output) { double('output').as_null_object }
let(:game) { Game.new(output) }
it "sends a welcome message" do
output.should_receive(:puts).with('Welcome to Codebreaker!')
game.start
end
it "prompts for the first guess" do
output.should_receive(:puts).with('Enter guess:')
game.start
end
end
end
end
对应于以下代码:
module Codebreaker
class Game
def initialize(output)
@output = output
end
def start
@output.puts 'Welcome to Codebreaker!'
@output.puts 'Enter a guess:'
end
end
end
由于我已将 :output 设置为 double.as_null_object,因此我希望它忽略它不期望的任何参数/方法。对于第一个测试(发送欢迎消息),它就是这样做的,它通过了。然而,第二个测试给了我这个错误:
Failure/Error: output.should_receive(:puts).with('Enter guess:')
Double "output" received :puts with unexpected arguments
expected: ("Enter guess:")
got: ("Welcome to Codebreaker!"), ("Enter a guess:")
# ./spec/codebreaker/game_spec.rb:16:in `block (3 levels) in <module:Codebreaker>'
为什么双重返回“欢迎使用 Codebreaker!” 和“输入猜测”,当我明确告诉它只期望“输入猜测:”时,我该如何解决这个问题,同时保持相同的设置/结构?