コード例 #1
0
        public async Task GetItemForUser_ShouldReturnBadRequest_IfRequestUserIdIsInvalid()
        {
            // Arrange
            var mockQueryHelper = new Mock <IQueryHelper>(MockBehavior.Strict);
            var controller      = new UserItemsController(mockQueryHelper.Object);
            int itemId          = 1;
            int userId          = 0;

            // Act
            IActionResult result = await controller.GetItemForUser(userId, itemId);

            // Assert
            var badRequestResult = Assert.IsType <BadRequestObjectResult>(result);

            Assert.Equal("Item in request is malformed / has invalid IDs", badRequestResult.Value);
        }
コード例 #2
0
        public async Task GetItemForUser_ShouldReturnNotFound_IfRequestUserCannotBeFound()
        {
            // Arrange
            var mockQueryHelper = new Mock <IQueryHelper>(MockBehavior.Strict);
            var controller      = new UserItemsController(mockQueryHelper.Object);
            int itemId          = 1;
            int userId          = 1;

            mockQueryHelper.Setup(q => q.UserQueries.GetUser(userId))
            .ReturnsAsync((UserDto)null);

            // Act
            IActionResult result = await controller.GetItemForUser(userId, itemId);

            // Assert
            var notFoundResult = Assert.IsType <NotFoundObjectResult>(result);

            Assert.Equal($"No user can be found with ID {userId}", notFoundResult.Value);
        }
コード例 #3
0
        public async Task GetItemForUser_ShouldReturnNotFound_IfMatchingItemsCannotBeFound()
        {
            // Arrange
            var mockQueryHelper = new Mock <IQueryHelper>(MockBehavior.Strict);
            var controller      = new UserItemsController(mockQueryHelper.Object);
            int userId          = 1;
            int itemId          = 1;

            mockQueryHelper.Setup(q => q.UserQueries.GetUser(userId))
            .ReturnsAsync(new UserDto());
            mockQueryHelper.Setup(q => q.UserItemQueries
                                  .GetMatchingItemsForUser(userId, It.Is <ItemDto>(i => i.Id == itemId)))
            .ReturnsAsync((UserItemDto)null);

            // Act
            IActionResult result = await controller.GetItemForUser(userId, itemId);

            // Assert
            var notFoundResult = Assert.IsType <NotFoundObjectResult>(result);

            Assert.Equal($"No items matched for request for user with ID {userId}", notFoundResult.Value);
        }
コード例 #4
0
        public async Task GetItemForUser_ShouldReturnResult_IfMatchingItemsCanBeFound()
        {
            // Arrange
            var mockQueryHelper = new Mock <IQueryHelper>(MockBehavior.Strict);
            var controller      = new UserItemsController(mockQueryHelper.Object);
            int userId          = 1;
            int itemId          = 1;

            mockQueryHelper.Setup(q => q.UserQueries.GetUser(userId))
            .ReturnsAsync(new UserDto());
            var expectedUserItem = new UserItemDto();

            mockQueryHelper.Setup(q => q.UserItemQueries
                                  .GetMatchingItemsForUser(userId, It.Is <ItemDto>(i => i.Id == itemId)))
            .ReturnsAsync(expectedUserItem);

            // Act
            IActionResult result = await controller.GetItemForUser(userId, itemId);

            // Assert
            var okResult = Assert.IsType <OkObjectResult>(result);

            Assert.Equal(expectedUserItem, okResult.Value);
        }