Пример #1
0
        public void Attack_Should_return_miss_when_input_not_in_range_or_ship_not_placed(bool isInRange,
                                                                                         bool isBoardCreated, string expected)
        {
            // Arrange
            var response = isBoardCreated ?
                           Tuple.Create <bool, BattleBoard>(isBoardCreated,
                                                            new BattleBoard()
            {
                BoardCells = new BoardCell[5, 5], Length = 2
            })
                : Tuple.Create <bool, BattleBoard>(isBoardCreated, null);

            cacheService.Setup(x => x.GetDataFromCache <BattleBoard>(It.IsAny <string>())).Returns(response);
            validationService.Setup(x => x.IsInRangeToAttack(It.IsAny <int>(), It.IsAny <int>())).Returns(isInRange);

            // Act
            var actual = trackerService.Attack(2, 3);

            // Assert
            Assert.AreEqual(expected, actual);
            validationService.Verify(x => x.IsInRangeToAttack(It.IsAny <int>(), It.IsAny <int>()), Times.Once);

            if (isInRange)
            {
                cacheService.Verify(x => x.GetDataFromCache <BattleBoard>(It.IsAny <string>()), Times.Once);
            }
            else
            {
                cacheService.Verify(x => x.GetDataFromCache <BattleBoard>(It.IsAny <string>()), Times.Never);
            }
        }
        public ActionResult <string> Attack(Coordinates coordinates)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var response = trackerService.Attack(coordinates.X, coordinates.Y);

            return(response);
        }