最后两个测试都单独工作......但是当两者都设置为运行(非待定)时,我会遇到问题。
问题:我可以创建一个将两者合并为一的测试吗?这看起来怎么样?(是的,我是rspec的新手)
require_relative '../spec_helper'
# the universe is vast and infinite....and...it is empty
describe "tic tac toe game" do
context "the game class" do
before (:each) do
player_h = Player.new("X")
player_c = Player.new("O")
@game = Game.new(player_h, player_c)
end
it "method drawgrid must return a 3x3 game grid" do
@game.drawgrid.should eq("\na #{$thegrid[:a1]}|#{$thegrid[:a2]}|#{$thegrid[:a3]} \n----------\nb #{$thegrid[:b1]}|#{$thegrid[:b2]}|#{$thegrid[:b3]} \n----------\nc #{$thegrid[:c1]}|#{$thegrid[:c2]}|#{$thegrid[:c3]} \n----------\n 1 2 3 \n")
@game.drawgrid
end
#FIXME - last two test here - how to merge into one?
it "play method must display 3x3 game grid" do
STDOUT.should_receive(:puts).and_return("\na #{$thegrid[:a1]}|#{$thegrid[:a2]}|#{$thegrid[:a3]} \n----------\nb #{$thegrid[:b1]}|#{$thegrid[:b2]}|#{$thegrid[:b3]} \n----------\nc #{$thegrid[:c1]}|#{$thegrid[:c2]}|#{$thegrid[:c3]} \n----------\n 1 2 3 \n").with("computer move")
@game.play
end
it "play method must display 3x3 game grid" do
STDOUT.should_receive(:puts).with("computer move")
@game.play
end
end
end
这里的信息只是包含播放方法的代码
require_relative "player"
#
#Just a Tic Tac Toe game class
class Game
#create players
def initialize(player_h, player_c)
#bring into existence the board and the players
@player_h = player_h
@player_c = player_c
#value hash for the grid lives here
$thegrid = {
:a1=>" ", :a2=>" ", :a3=>" ",
:b1=>" ", :b2=>" ", :b3=>" ",
:c1=>" ", :c2=>" ", :c3=>" "
}
#make a global var for drawgrid which is used by external player class
$gamegrid = drawgrid
end
#display grid on console
def drawgrid
board = "\n"
board << "a #{$thegrid[:a1]}|#{$thegrid[:a2]}|#{$thegrid[:a3]} \n"
board << "----------\n"
board << "b #{$thegrid[:b1]}|#{$thegrid[:b2]}|#{$thegrid[:b3]} \n"
board << "----------\n"
board << "c #{$thegrid[:c1]}|#{$thegrid[:c2]}|#{$thegrid[:c3]} \n"
board << "----------\n"
board << " 1 2 3 \n"
return board
end
#start the game
def play
#draw the board
puts drawgrid
#external call to player class
@player = @player_c.move_computer("O")
end
end
player_h = Player.new("X")
player_c = Player.new("O")
game = Game.new(player_h, player_c)
game.play
更新 - 测试错误输出 只是为了完整起见....这是运行rspec spec ...
的完整输出 gideon@thefonso ~/Documents/ca_ruby/rubytactoe (now-with-rspec)$ rspec spec
a | |
----------
b | |
----------
c | |
----------
1 2 3
computer move
tic tac toe game
the game class
method drawgrid must return a 3x3 game grid
An error occurred in an after(:each) hook
RSpec::Mocks::MockExpectationError: (#<IO:0x007f948406fcf0>).puts(any args)
expected: 1 time
received: 0 times
occurred at /Users/gideon/Documents/ca_ruby/rubytactoe/spec/game_spec.rb:18:in `block (3 levels) in <top (required)>'
play method must display 3x3 game grid (FAILED - 1)
An error occurred in an after(:each) hook
RSpec::Mocks::MockExpectationError: (#<IO:0x007f948406fcf0>).puts("computer move")
expected: 1 time
received: 0 times
occurred at /Users/gideon/Documents/ca_ruby/rubytactoe/spec/game_spec.rb:22:in `block (3 levels) in <top (required)>'
play method must display 3x3 game grid (FAILED - 2)
tic tac toe game
the player class
must have a human player X
must have a computer player O
Failures:
1) tic tac toe game the game class play method must display 3x3 game grid
Failure/Error: STDOUT.should_receive(:puts).and_return("\na #{$thegrid[:a1]}|#{$thegrid[:a2]}|#{$thegrid[:a3]} \n----------\nb #{$thegrid[:b1]}|#{$thegrid[:b2]}|#{$thegrid[:b3]} \n----------\nc #{$thegrid[:c1]}|#{$thegrid[:c2]}|#{$thegrid[:c3]} \n----------\n 1 2 3 \n").with("computer move")
NoMethodError:
undefined method `with' for #<Proc:0x007f9484341168>
# ./spec/game_spec.rb:18:in `block (3 levels) in <top (required)>'
2) tic tac toe game the game class play method must display 3x3 game grid
Failure/Error: @game.play
#<IO:0x007f948406fcf0> received :puts with unexpected arguments
expected: ("computer move")
got: ("\na | | \n----------\nb | | \n----------\nc | | \n----------\n 1 2 3 \n")
# ./lib/game.rb:37:in `puts'
# ./lib/game.rb:37:in `play'
# ./spec/game_spec.rb:23:in `block (3 levels) in <top (required)>'
Finished in 0.00457 seconds
5 examples, 2 failures
Failed examples:
rspec ./spec/game_spec.rb:17 # tic tac toe game the game class play method must display 3x3 game grid
rspec ./spec/game_spec.rb:21 # tic tac toe game the game class play method must display 3x3 game grid
gideon@thefonso ~/Documents/ca_ruby/rubytactoe (now-with-rspec)$
答案 0 :(得分:1)
问题应该包括你得到的“问题”的种;他们帮助诊断出了什么问题。
这是 的答案,之后有一个警告;还有其他方法。
it "play method must display 3x3 game grid" do
STDOUT.should_receive(:puts).with("\na #{$thegrid[:a1]}|#{$thegrid[:a2]}|#{$thegrid[:a3]} \n----------\nb #{$thegrid[:b1]}|#{$thegrid[:b2]}|#{$thegrid[:b3]} \n----------\nc #{$thegrid[:c1]}|#{$thegrid[:c2]}|#{$thegrid[:c3]} \n----------\n 1 2 3 \n").twice.ordered
STDOUT.should_receive(:puts).with("computer move").once.ordered
@game.play
@game.play
end
警告:你现在测试的方式将无法维持。我可能不继续沿着移动测试期间检查输出的路径,而不是单独测试输出,但是隔离移动机制,以便你可以检查一个数组值,或者一个标志,指示谁移动它是,依此类推。
您还需要决定是否确实要测试顺序移动,而不是移动计算逻辑。顺序移动需要如上所述的刺激性测试结构,或强制执行测试顺序,或以其他方式确保移动顺序正确。
重新考虑一下,特别是你正在测试什么,以及什么时候,这可能更有意义。