2012-09-04 2 views
0

마지막 두 테스트 작업은 개별적으로 실행되지만 둘 다 실행 (비 보류)으로 설정되면 문제가 발생합니다.두 개의 테스트를 하나의 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)$ 

답변

1

질문은 무엇 가지 "프로 포함해야한다입니다 당신이 얻는 "blems; 그들은 잘못 된 것을 진단하는 데 도움을줍니다.

여기에 답변이 있으며 이후에주의해야합니다. 다른 방법이 있습니다.

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 

경고 : 현재 테스트중인 방식은 유지가 불가능합니다. 나는 아마도 이 아닐 것이다. 이동 테스트 중에 출력을 검사하는 경로를 계속 진행한다. 출력을 별도로 테스트 할 것이지만 이동 메커니즘을 격리하여 배열 값이나 이동을 나타내는 플래그를 확인할 수있다. 곧.

계산 논리를 이동하는 대신 순차 이동을 실제로 테스트 할 것인지 결정할 수도 있습니다. 순차적 인 이동은 위와 같이 자극적 인 테스트 구성을 필요로하거나 테스트 순서를 시행하거나 이동 순서가 올바른지 확인해야합니다.

특히 무엇을 테스트하고 있는지 다시 생각해 보는 것이 더 효과적 일 수 있습니다.

+0

아, 예, 그렇습니다. 무엇이 구체적으로 테스트되는지. :) 저는 RSpec을 처음 사용하기 때문에 가능한 한 기본으로 유지하려고 노력할 것입니다. 그러나 "기본"방향으로 가고 있다는 것을 아는 것은 그것에 대한 나의 새로운 점을 알기가 불가능합니다. 어려움의 또 다른 층. 답변 해 주셔서 감사합니다. – thefonso

관련 문제