public void PositionOnGrid_ShouldRemoveHandlerForSquaresThatAreNotOccupiedAnymore() { //Arrange var originalGridSquareArrayBuilder = new GridSquareArrayBuilder(_kind); Mock <IGridSquare>[] originalSquareMocks = originalGridSquareArrayBuilder.BuildMockArray(); foreach (var squareMock in originalSquareMocks) { squareMock.Setup(g => g.HitByBomb()).Callback(() => { squareMock.Raise(s => s.OnHitByBomb += null, squareMock.Object); }); } _ship.PositionOnGrid(originalGridSquareArrayBuilder.BuildArray()); //Act _ship.PositionOnGrid(new GridSquareArrayBuilder(_kind).BuildArray()); //Assert foreach (var squareMock in originalSquareMocks) { squareMock.Object.HitByBomb(); squareMock.VerifySet(s => s.Status = It.IsAny <GridSquareStatus>(), Times.Never, "When a GridSquare is hit and the square is not occupied by a ship anymore, " + "the ship should not handle the OnHitByBomb event of the GridSquare anymore. " + "Use the '-=' operator on the 'OnHitByBomb' event to remove the event handler."); } }
public void HasSunk_ShouldReturnFalseWhenNotAllSquaresAreHit() { //Arrange var gridSquareArrayBuilder = new GridSquareArrayBuilder(_kind).WithStatus(GridSquareStatus.Hit); Mock <IGridSquare>[] squareMocks = gridSquareArrayBuilder.BuildMockArray(); Mock <IGridSquare> untouchedSquareMock = squareMocks.NextRandomElement(); untouchedSquareMock.SetupGet(s => s.Status).Returns(GridSquareStatus.Untouched); IGridSquare[] squares = gridSquareArrayBuilder.BuildArray(); _ship.PositionOnGrid(squares); //Act + Assert Assert.That(_ship.HasSunk, Is.False); }
public void PositionOnGrid_ShouldAddAHandlerForWhenASquareIsHit() { //Arrange var gridSquareArrayBuilder = new GridSquareArrayBuilder(_kind); Mock <IGridSquare>[] squareMocks = gridSquareArrayBuilder.BuildMockArray(); Mock <IGridSquare> squareToHitMock = squareMocks.NextRandomElement(); IGridSquare squareToHit = squareToHitMock.Object; squareToHitMock.Setup(g => g.HitByBomb()).Callback(() => { squareToHitMock.Raise(s => s.OnHitByBomb += null, squareToHitMock.Object); }); //Act _ship.PositionOnGrid(gridSquareArrayBuilder.BuildArray()); squareToHit.HitByBomb(); //Assert squareToHitMock.VerifySet(s => s.Status = GridSquareStatus.Hit, Times.Once, "When a GridSquare is hit and the square is occupied by a ship, " + "the ship should handle the OnHitByBomb event of the GridSquare and set its status to Hit."); }