ruby - rspec puts output test -
i'm having trouble understanding how test output puts
. need know need in rspec file.
this rspec file:
require 'game_io' require 'board' describe gameio before(:each) @gameio = gameio.new @board = board.new end context 'welcome_message' 'should display welcome message' 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 game. ready defeated" end end end
this file testing against:
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 game. ready defeated" end def output(msg) @stdout.puts msg end def input @stdin.gets end end
note: updated rspec code reflect changes made test file given suggestions found elsewhere. resolve poblem completly used changes suggested chris heald in main file. thank , thank chris.
your initializer should be:
def initialize(game_input = $stdin, game_output = $stdout) @game_input = game_input @game_output = game_output end
the reason attr_accessor
generates methods this:
# attr_accessor :game_output def game_output @game_output end def game_output=(output) @game_output = output end
(attr_reader generates reader method)
thus, since never assign @game_output
, game_output
method return nil.
Comments
Post a Comment