public void Delete_ShouldReturnSuccess() { var testDbMessage = TestData.CreateDbMessage("message title", "Message description", 0); context.Message.Add(testDbMessage); context.SaveChanges(); var result = controller.DeleteMessage(testDbMessage.Id) as StatusCodeResult; result.Should().BeOfType <NoContentResult>(); result.StatusCode.Should().Be(204); }
public async Task DeleteMessage_NotAuthorizedUserIdPassed_ReturnsUnauthorizedResult() { var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, "1"), })); var mapperMock = new Mock <IMapper>(); var repositoryMock = new Mock <IRepositoryWrapper>(); repositoryMock.Setup(r => r.MessageRepository.GetMessageAsync(It.IsAny <int>())).ReturnsAsync(new Message()); repositoryMock.Setup(r => r.SaveAllAsync()).ReturnsAsync(true); var controllerMock = new MessagesController(repositoryMock.Object, mapperMock.Object); controllerMock.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = user } }; var result = await controllerMock.DeleteMessage(2, 2); Assert.IsType <UnauthorizedResult>(result); }
public async Task DeleteMessage_ShouldNotBeFound() { Message messageToDelete = GetMessage(); _messageRepo.Setup(repo => repo.FindMessageAsync(messageToDelete.MessageId)) .Returns(Task.FromResult((Message)null)); var controller = new MessagesController(_messageRepo.Object); var response = await controller.DeleteMessage(messageToDelete.MessageId); Assert.IsInstanceOf <NotFoundResult>(response); }
public void Delete_ShouldReturnBadRequest() { var mockContext = new Mock <HubDbContext>(options); mockContext.Setup(m => m.Message).Throws(new Exception()); var badController = new MessagesController(mockContext.Object, logger.Object, mapper); var result = badController.DeleteMessage(Guid.NewGuid()) as ObjectResult; result.Should().BeOfType <BadRequestObjectResult>(); result.StatusCode.Should().Be(400); }
public async void DeleteMessage_ReturnsBadRequestObjectResult_WhenTheMessageGuidIdIsInvalid() { //Arrange var controller = new MessagesController(_loggerMock.Object, _mockMessagesService.Object, _mockUserService.Object); //Act var result = await controller.DeleteMessage(ConstIds.InvalidGuid); //Assert var badRequestObjectResult = Assert.IsType <BadRequestObjectResult>(result); Assert.Equal($"{ConstIds.InvalidGuid} is not valid guid.", badRequestObjectResult.Value); }
public async Task DeleteMessage_ShouldDeleteMessage() { Message messageToDelete = GetMessage(); _messageRepo.Setup(repo => repo.FindMessageAsync(messageToDelete.MessageId)) .Returns(Task.FromResult(messageToDelete)); var controller = new MessagesController(_messageRepo.Object); await controller.DeleteMessage(messageToDelete.MessageId); _messageRepo.Verify(mock => mock.FindMessageAsync(messageToDelete.MessageId), Times.Once()); _messageRepo.Verify(mock => mock.RemoveMessage(messageToDelete), Times.Once()); _messageRepo.Verify(mock => mock.SaveChangesAsync(), Times.Once()); }
public async void DeleteMessage_ReturnsNoContentResult_WhenTheMessageHasBeenRemoved() { //Arrange _mockMessagesService.Setup(Service => Service.DeleteMessageAsync(It.IsAny <Guid>())) .Verifiable(); var controller = new MessagesController(_loggerMock.Object, _mockMessagesService.Object, _mockUserService.Object); //Act var result = await controller.DeleteMessage(ConstIds.ExampleMessageId); //Act var noContentResult = Assert.IsType <NoContentResult>(result); _mockMessagesService.Verify(); }
public async void DeleteMessage_ReturnsInternalServerErrorResult_WhenExceptionThrownInService() { //Arrange _mockMessagesService.Setup(Service => Service.DeleteMessageAsync(It.IsAny <Guid>())) .Throws(new ArgumentNullException(nameof(Guid))) .Verifiable(); var controller = new MessagesController(_loggerMock.Object, _mockMessagesService.Object, _mockUserService.Object); //Act var result = await controller.DeleteMessage(ConstIds.ExampleMessageId); //Assert var internalServerErrorResult = Assert.IsType <StatusCodeResult>(result); Assert.Equal(StatusCodes.Status500InternalServerError, internalServerErrorResult.StatusCode); _mockMessagesService.Verify(); }