我無法理解如何使用puts
測試輸出。我需要知道我需要在我的RSPEC文件中做什麼。rspec輸出測試
這是我RSPEC文件:
require 'game_io'
require 'board'
describe GameIO do
before(:each) do
@gameio = GameIO.new
@board = Board.new
end
context 'welcome_message' do
it 'should display a welcome message' do
test_in = StringIO.new("some test input\n")
test_out = StringIO.new
test_io = GameIO.new(test_in, test_out)
test_io.welcome_message
test_io.game_output.string.should == "Hey, welcome to my game. Get ready to be defeated"
end
end
end
這是該文件是針對測試:
class GameIO
attr_reader :game_input, :game_output
def initialize(game_input = $stdin, game_output = $stdout)
@stdin = game_input
@stdout = game_output
end
def welcome_message
output "Hey, welcome to my game. Get ready to be defeated"
end
def output(msg)
@stdout.puts msg
end
def input
@stdin.gets
end
end
注:我更新了我的RSPEC代碼,以反映我對給我的測試文件所做的更改在別處找到的建議爲了完全解決這個問題,我使用了我的主文件中Chris Heald提出的修改。謝謝大家,謝謝克里斯。
以供將來參考:一旦你已經解決了這個問題,這是更好地把解決方案中的答案,而不是編輯你的問題。如果有人回到這個問題,很難看出原來的問題是什麼。 :) – henrikhodne