public void ShootAtOpponent_ShouldUseServiceToRegisterTheShot() { //Arrange Guid gameId = Guid.NewGuid(); GridCoordinateModel coordinateModel = new GridCoordinateModelBuilder().Build(); ShotResult expectedShotResult = ShotResult.CreateMissed(); _gameServiceMock.Setup(service => service.ShootAtOpponent(It.IsAny <Guid>(), It.IsAny <Guid>(), It.IsAny <GridCoordinate>())) .Returns(expectedShotResult); //Act var result = _controller.ShootAtOpponent(gameId, coordinateModel).Result as OkObjectResult; //Assert Assert.That(result, Is.Not.Null, "An instance of 'OkObjectResult' should be returned."); _userManagerMock.Verify(manager => manager.GetUserAsync(It.IsAny <ClaimsPrincipal>()), Times.Once, "The 'GetUserAsync' of the UserManager is not called"); _gameServiceMock.Verify(service => service.ShootAtOpponent(gameId, _loggedInUser.Id, It.Is <GridCoordinate>( gc => gc.Row == coordinateModel.Row && gc.Column == coordinateModel.Column)), Times.Once, "The 'ShootAtOpponent' of the IGameService is not called correctly."); Assert.That(result.Value, Is.SameAs(expectedShotResult)); }
public void ShootAtOpponent_ShouldLetTheComputerShootWhenAllBombsAreShot() { AssertThatGameIsConstructed(); //Arrange var shootingStrategyMock = new Mock <IShootingStrategy>(); IPlayer computerPlayer = new ComputerPlayer(_settings, shootingStrategyMock.Object); _player2Builder.WithBombsLoaded(true); IPlayer humanPlayer = _player2; _game = new Game(_settings, computerPlayer, humanPlayer); _game.Start(); GridCoordinate targetCoordinate = new GridCoordinateBuilder().Build(); Mock <IPlayer> humanPlayerMock = _player2Builder.BuildMock(); ShotResult expectedShotResult = ShotResult.CreateMissed(); humanPlayerMock.Setup(p => p.ShootAt(It.IsAny <IPlayer>(), It.IsAny <GridCoordinate>())) .Returns(() => { _player2Builder.WithBombsLoaded(false); return(expectedShotResult); }); //Act ShotResult result = _game.ShootAtOpponent(_player2.Id, targetCoordinate); humanPlayerMock.Verify(p => p.ShootAt(computerPlayer, targetCoordinate), Times.Once, "The ShootAt method of the human player is not called correctly."); shootingStrategyMock.Verify(s => s.DetermineTargetCoordinate(), Times.AtLeast(1), "The computer player did not shoot. You must call the ShootAutomatically method of the computer player. " + "The ShootAutomatically method should in turn call the DetermineTargetCoordinate method of the shooting strategy of the computer. " + "This test fails because no call to the DetermineTargetCoordinate method is detected."); humanPlayerMock.Verify(p => p.ReloadBombs(), Times.Once, "The ReloadBombs method of the human player should be called after the computer is done with shooting."); Assert.That(result, Is.SameAs(expectedShotResult), "The ShotResult returned by the ShootAt method (of the human player) should be returned."); }
public void ShootAtOpponent_ShouldUseTheShootAtMethodOfThePlayer() { //Arrange GridCoordinate targetCoordinate = new GridCoordinateBuilder().Build(); _player1Builder.WithBombsLoaded(true); ShotResult expectedShotResult = ShotResult.CreateMissed(); Mock <IPlayer> player1Mock = _player1Builder.BuildMock(); player1Mock.Setup(p => p.ShootAt(It.IsAny <IPlayer>(), It.IsAny <GridCoordinate>())) .Returns(expectedShotResult); _game.Start(); //Act ShotResult result = _game.ShootAtOpponent(_player1.Id, targetCoordinate); player1Mock.Verify(p => p.ShootAt(_player2, targetCoordinate), Times.Once, "The ShootAt method of the player that shoots is not called correctly."); Assert.That(result, Is.SameAs(expectedShotResult), "The ShotResult returned by the ShootAt method should be returned."); }
public void ShootAtOpponent_ShouldUseServiceToRegisterTheShot() { //Arrange Guid gameId = Guid.NewGuid(); GridCoordinateModel coordinateModel = new GridCoordinateModelBuilder().Build(); ShotResult expectedShotResult = ShotResult.CreateMissed(); _gameServiceMock.Setup(service => service.ShootAtOpponent(gameId, _loggedInUser.Id, It.Is <GridCoordinate>( gc => gc.Row == coordinateModel.Row && gc.Column == coordinateModel.Column))) .Returns(expectedShotResult); //Act var result = _controller.ShootAtOpponent(gameId, coordinateModel).Result as OkObjectResult; //Assert Assert.That(result, Is.Not.Null, "An instance of 'OkObjectResult' should be returned."); _userManagerMock.Verify(); _gameServiceMock.Verify(); Assert.That(result.Value, Is.SameAs(expectedShotResult)); }
public ShotResult ShootAt(IPlayer opponent, GridCoordinate coordinate) { if (HasBombsLoaded == true) { this._HasBombsLoaded = false; IGridSquare square = opponent.Grid.Shoot(coordinate); opponent.ReloadBombs(); if (square.Status == GridSquareStatus.Hit) { return(ShotResult.CreateHit(opponent.Fleet.FindShipAtCoordinate(coordinate), this.settings.MustReportSunkenShip)); } if (square.Status == GridSquareStatus.Miss) { return(ShotResult.CreateMissed()); } return(ShotResult.CreateMissed()); } else { return(ShotResult.CreateMisfire("You have no bombs loaded")); } }
public ShotResult ShootAt(IPlayer opponent, GridCoordinate coordinate) { /* Get the grid of the opponent & shoot */ IGridSquare square = opponent.Grid.Shoot(coordinate); /* Bomb has been shot */ this.nrOfBombsLoaded = 0; /* The grid square informas about the result of the shot */ if (square.Status == GridSquareStatus.Hit) { /* Ship hit */ return(ShotResult.CreateHit(opponent.Fleet.FindShipAtCoordinate(coordinate), mustReportSunkenShip)); } else if (square.Status == GridSquareStatus.Miss) { /* Missed */ return(ShotResult.CreateMissed()); } else { return(ShotResult.CreateMisfire("out of bound")); } }