Beispiel #1
0
        public void AddShip_OccupiedByAnotherShip()
        {
            using (AutoMock mock = AutoMock.GetLoose())
            {
                string expectedErrorMsg = "You can't add ship on the location because it's occupied by another ship";
                mock.Mock <IBattleshipRepository>()
                .Setup(x => x.GetGameById(1))
                .ReturnsAsync(CreateSomeGames().FirstOrDefault(x => x.GameId == 1));
                ShipForAddingDto shipForAddingDto = new ShipForAddingDto()
                {
                    StartRow     = 5,
                    StartColumn  = 1,
                    ShipLength   = 3,
                    IsVertically = true
                };
                GameService cls = mock.Create <GameService>();

                var ex = Record.ExceptionAsync(() => cls.AddShip(1, shipForAddingDto)).Result;

                Assert.NotNull(ex);
                Assert.IsType <Exception>(ex);
                if (ex is Exception exception)
                {
                    Assert.Equal(expectedErrorMsg, exception.Message);
                }
            }
        }
Beispiel #2
0
        public void AddShip_GameNotExist()
        {
            using (AutoMock mock = AutoMock.GetLoose())
            {
                string expectedErrorMsg = $"the game 10 does not exist, adding this ship failed";
                mock.Mock <IBattleshipRepository>()
                .Setup(x => x.GetGameById(10))
                .ReturnsAsync(CreateSomeGames().FirstOrDefault(x => x.GameId == 100));
                ShipForAddingDto shipForAddingDto = new ShipForAddingDto()
                {
                    StartRow     = 1,
                    StartColumn  = 1,
                    ShipLength   = 3,
                    IsVertically = true
                };
                GameService cls = mock.Create <GameService>();

                var ex = Record.ExceptionAsync(() => cls.AddShip(10, shipForAddingDto)).Result;

                Assert.NotNull(ex);
                Assert.IsType <Exception>(ex);
                if (ex is Exception exception)
                {
                    Assert.Equal(expectedErrorMsg, exception.Message);
                }
            }
        }
Beispiel #3
0
        public void AddShip_NotEntireOnTheBoard(int startRow, int startColumn, int shipLengh, bool isVertically)
        {
            using (AutoMock mock = AutoMock.GetLoose())
            {
                string expectedErrorMsg = $"This ship is not entirely on the board";
                mock.Mock <IBattleshipRepository>()
                .Setup(x => x.GetGameById(1))
                .ReturnsAsync(CreateSomeGames().FirstOrDefault(x => x.GameId == 1));
                ShipForAddingDto shipForAddingDto = new ShipForAddingDto()
                {
                    StartRow     = startRow,
                    StartColumn  = startColumn,
                    ShipLength   = shipLengh,
                    IsVertically = isVertically
                };
                GameService cls = mock.Create <GameService>();

                var ex = Record.ExceptionAsync(() => cls.AddShip(1, shipForAddingDto)).Result;

                Assert.NotNull(ex);
                Assert.IsType <Exception>(ex);
                if (ex is Exception exception)
                {
                    Assert.Equal(expectedErrorMsg, exception.Message);
                }
            }
        }
Beispiel #4
0
        public void AddShip_Success(int startRow, int startColumn, int shipLength, bool isVertically)
        {
            using (AutoMock mock = AutoMock.GetLoose())
            {
                mock.Mock <IBattleshipRepository>()
                .Setup(x => x.GetGameById(1))
                .ReturnsAsync(CreateSomeGames().FirstOrDefault(x => x.GameId == 1));
                mock.Mock <IBattleshipRepository>()
                .Setup(x => x.SaveAll())
                .ReturnsAsync(true);
                ShipForAddingDto shipForAddingDto = new ShipForAddingDto()
                {
                    StartRow     = startRow,
                    StartColumn  = startColumn,
                    ShipLength   = shipLength,
                    IsVertically = isVertically
                };
                GameService cls = mock.Create <GameService>();

                cls.AddShip(1, shipForAddingDto).Wait();

                mock.Mock <IBattleshipRepository>()
                .Verify(x => x.SaveAll(), Times.Exactly(1));
            }
        }
Beispiel #5
0
        public void Can_Add_Ship_To_Empty_Square_Successfully()
        {
            // Given
            int startingRow    = 0;
            int startingColumn = 1;
            int shipId         = 10;

            _gameService.AddShip(shipId, 3, startingRow, startingColumn, OrientationType.Horizontal, _player);

            // When
            var currentSquare = _player.PlayerGameBoard.GetItem(startingRow, startingColumn);

            // Then
            Assert.AreEqual(1, _player.Ships.Count);
            Assert.AreEqual(shipId, currentSquare.GetSquareContent());
        }