我很难为我的 draw_three_by_three 方法创建测试......这是我的代码......下面我将列出我的测试代码不起作用......你能帮我解决这个问题吗?
class GameIO
attr_reader :stdin, :stdout
def initialize(stdin = $stdin, stdout = $stdout)
@stdin = stdin
@stdout = stdout
end
def draw_three_by_three(board)
out_board = "\n"
out_board << " #{board.grid[0]} | #{board.grid[1]} | #{board.grid[2]}\n"
out_board << "-----------\n"
out_board << " #{board.grid[3]} | #{board.grid[4]} | #{board.grid[5]}\n"
out_board << "-----------\n"
out_board << " #{board.grid[6]} | #{board.grid[7]} | #{board.grid[8]} \n"
output out_board
end
def output(msg)
stdout.puts msg
end
end
这是我的 rspec 代码错误...我该如何为此编写 rspec 测试?
require 'game_io'
require 'board'
describe 'game_io' do
before(:each) do
@gameio = GameIO.new
@board = Board.new
end
context 'draw_three_by_three_board' do
it 'should display the board on standard output' do
@gameio.draw_three_by_three(@board).should == <<-EOF.gsub(/^ {6}/, '')
+ | + | +
-----------
+ | + | +
-----------
+ | + | +
EOF
end
end
end
这是我得到的 rspec 错误....
$ rspec 规范/game_io_spec.rb
+ | + | +
-----------
+ | + | +
-----------
+ | + | +
F
失败:
1) game_io draw_three_by_three_board should display the board on standard output
Failure/Error: EOF
expected: "\n + | + | +\n-----------\n + | + | +\n-----------\n + | + | + \n"
got: nil (using ==)
# ./spec/game_io_spec.rb:20:in `block (3 levels) in <top (required)>'
在 0.00074 秒内完成 1 个示例,1 个失败