마지막 두 테스트 작업은 개별적으로 실행되지만 둘 다 실행 (비 보류)으로 설정되면 문제가 발생합니다.두 개의 테스트를 하나의 RSpec에 병합하는 방법
질문 : 둘을 하나로 병합하는 테스트를 만들 수 있습니까? 그냥 완전성을 위해 테스팅 오류 출력 - 어떻게 이런 일이 (예, 나는 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
UPDATE를 포함하는 코드 보일 것이다. ... 여기 실행 RSpec에 사양에서 최대 출력 ...
[email protected] ~/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
[email protected] ~/Documents/ca_ruby/rubytactoe (now-with-rspec)$
는
아, 예, 그렇습니다. 무엇이 구체적으로 테스트되는지. :) 저는 RSpec을 처음 사용하기 때문에 가능한 한 기본으로 유지하려고 노력할 것입니다. 그러나 "기본"방향으로 가고 있다는 것을 아는 것은 그것에 대한 나의 새로운 점을 알기가 불가능합니다. 어려움의 또 다른 층. 답변 해 주셔서 감사합니다. – thefonso