Rspec editing the subject of a test


#1

Hi All,

In rspec how can you make changes to the subject so you can test things further. For example:



 describe "#right_coloured_piece?" do
        
        it "returns true on a white piece when the turn is white" do
            expect(game.right_coloured_piece?(:a2)).to eq true
        end

        it "returns false on a black piece when the turn is white" do
            expect(game.right_coloured_piece?(:h7)).to eq false
        end
        
        it "returns true on a black piece when the turn is black" do
           
            expect(game.right_coloured_piece?(:h7)).to eq true
        end

        it "returns false on a white piece when the turn is black" do
            
            expect(game.right_coloured_piece?(:a2)).to eq false
        end
    end

How do I change the turn on the board from :white to :black so that I can run the last two tests?

turn is an attribute of board which is itself an attribute of game.


#2

You could wrap those two tests in an rspec context and then inside that call the method that changes the turn.


#3

Hey couch thanks for the advice.

As it turns out I was able to change the colour of the turn the normal way: game.board.turn = :black I just had to make the change in each it block.