【发布时间】:2014-05-12 23:33:09
【问题描述】:
我现在正在尝试遵循 RSpec 书,但对我来说似乎不是很清楚。它有一个 lib/codebreaker/game.rb 文件:
module Codebreaker
class Game
def initialize(output)
@output = output
end
def start
@output.puts "Welcome to Codebreaker!"
end
end
end
我有一个 spec/codebreaker/game_spec.rb 文件:
require 'spec_helper'
module Codebreaker
describe Game do
describe "#start" do
it "sends a welcome message" do
output = double("output")
game = Game.new(output)
output.should_receive(:puts).with("Welcome to Codebreaker!")
game.start
end
it "prompts for the first guess"
end
end
end
所以目前,“它'发送欢迎消息'”测试通过,但我不明白为什么。
game_spec 文件说“输出”应该接收带有“欢迎使用 Codebreaker!”的 puts 命令。但是,在 game.rb 文件中,我们清楚地看到输出是初始化方法的参数。 @outputs,实例变量而不是参数参数,有“欢迎使用 Codebreaker!”叫它。为什么当明显输出没有接收到:puts,但@output 接收到时,这个测试应该通过?
如果 @output.should_receive(:puts).with("Welcome to Codebreaker!") 传递,我会理解,但输出只是传递给初始化的参数。 RSpec 如何知道输出实际上是@output 甚至可以访问@output?
【问题讨论】:
标签: ruby-on-rails ruby rspec bdd