public void ReturnsBadRequestWhenModelStateIsInvalid() { // Arrange var battleshipOptions = new BattleshipOptions { Alignment = BattleShip.Horizontal, Column = 1, Row = 0, ShipSize = 4, OpponentId = 2 }; _mockBattleshipUtility.Setup(m => m.AddBattleship(It.IsAny <Cell[][]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns((BattleshipUtilityResult)null); Cell[][] playerBoard = null; var memoryCache = MockMemoryCacheService.GetMockedMemoryCache(); memoryCache.Set(0, playerBoard); var battleshipController = new BattleshipController(memoryCache, _mockBattleshipUtility.Object); // Act battleshipController.ModelState.AddModelError("Bad", "Request"); var subject = battleshipController.Add(battleshipOptions); // Assert var badRequestObjectResult = subject.Result as Microsoft.AspNetCore.Mvc.BadRequestObjectResult; Assert.IsNull(subject.Value); Assert.AreEqual(badRequestObjectResult.StatusCode.Value, StatusCodes.Status400BadRequest); }
public void ReturnsValidBattleship() { // Arrange var battleshipOptions = new BattleshipOptions { Alignment = BattleShip.Horizontal, Column = 1, PlayerId = 1, Row = 1, ShipSize = 4, OpponentId = 2 }; _mockBattleshipUtility.Setup( m => m.AddBattleship( It.IsAny <Cell[][]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())) .Returns(new BattleshipUtilityResult("Battleship added", BattleshipResultType.Added)); var memoryCache = MockMemoryCacheService.GetMemoryCache(BattleshipUtilityTestHelpers.CreateDefaultBoard()); var battleshipController = new BattleshipController(memoryCache, _mockBattleshipUtility.Object); // Act var subject = battleshipController.Add(battleshipOptions); // Assert Assert.IsNotNull(subject); Assert.AreEqual(subject.Value.ResultType, BattleshipResultType.Added); }
public void ReturnsNotFoundWhenPlayerboardIsNull() { // Arrange var battleshipOptions = new BattleshipOptions { Alignment = BattleShip.Horizontal, Column = 1, PlayerId = 1, Row = 1, ShipSize = 4, OpponentId = 2 }; _mockBattleshipUtility.Setup(m => m.AddBattleship(It.IsAny <Cell[][]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())).Returns((BattleshipUtilityResult)null); Cell[][] playerBoard = null; var memoryCache = MockMemoryCacheService.GetMockedMemoryCache(); memoryCache.Set(0, playerBoard); var battleshipController = new BattleshipController(memoryCache, _mockBattleshipUtility.Object); // Act var subject = battleshipController.Add(battleshipOptions); // Assert var notFoundObjectResult = subject.Result as Microsoft.AspNetCore.Mvc.NotFoundObjectResult; Assert.IsNull(subject.Value); Assert.AreEqual(notFoundObjectResult.StatusCode.Value, StatusCodes.Status404NotFound); }