public async void GetMessages_ReturnsBadRequestObjectResult_WhenTheUserOrWithWhoIdIsInvalid(string testUserId, string testFriendId) { //Arrange var controller = new MessagesController(_loggerMock.Object, _mockMessagesService.Object, _mockUserService.Object); //Act var result = await controller.GetMessagesWith(testUserId, testFriendId, _paginationParams); //Assert var badRequestObjectResult = Assert.IsType <BadRequestObjectResult>(result.Result); Assert.Equal($"{testUserId} or user: {testFriendId} is not valid guid.", badRequestObjectResult.Value); }
public async void GetMessages_ReturnsInternalServerErrorResult_WhenExceptionThrownInService() { //Arrange _mockUserService.Setup(Service => Service.CheckIfUserExists(new Guid(ConstIds.ExampleUserId))) .Throws(new ArgumentNullException(nameof(Guid))) .Verifiable(); var controller = new MessagesController(_loggerMock.Object, _mockMessagesService.Object, _mockUserService.Object); //Act var result = await controller.GetMessagesWith(ConstIds.ExampleUserId, ConstIds.ExampleFromWhoId, _paginationParams); //Assert var internalServerErrorResult = Assert.IsType <StatusCodeResult>(result.Result); Assert.Equal(StatusCodes.Status500InternalServerError, internalServerErrorResult.StatusCode); _mockUserService.Verify(); }
public async void GetMessagesWith_ReturnsOkObjectResult_WithAPagedListOfMessagesData() { //Arrange var messages = new List <MessageDto>(); _fixture.AddManyTo(messages, 15); var pagedList = PagedList <MessageDto> .Create(messages, _paginationParams.PageNumber, _paginationParams.PageSize, _paginationParams.Skip); _mockUserService.Setup(Service => Service.CheckIfUserExists(It.IsAny <Guid>())) .ReturnsAsync(true) .Verifiable(); _mockMessagesService.Setup(Service => Service.GetUserMessagesWith(It.IsAny <Guid>(), It.IsAny <Guid>(), _paginationParams)) .Returns(pagedList) .Verifiable(); var mockUrlHelper = new Mock <IUrlHelper>(); mockUrlHelper .Setup(m => m.Link("GetMessagesWith", It.IsAny <object>())) .Returns("some url") .Verifiable(); var controller = new MessagesController(_loggerMock.Object, _mockMessagesService.Object, _mockUserService.Object) { Url = mockUrlHelper.Object }; //Act var result = await controller.GetMessagesWith(ConstIds.ExampleUserId, ConstIds.ExampleFromWhoId, _paginationParams); //Assert var actionResult = Assert.IsType <OkObjectResult>(result.Result); var model = Assert.IsType <CollectionWithPaginationData <MessageDto> >(actionResult.Value); Assert.Equal(_paginationParams.PageSize, model.Collection.Count); _mockUserService.Verify(); _mockMessagesService.Verify(); }
public async void GetMessages_ReturnsNotFoundObjectResult_WhenTheUserOrWithWhoDoesntExist(bool doesTheUserExist, bool doesTheFriendExist) { //Arrange _mockUserService.Setup(Service => Service.CheckIfUserExists(It.IsAny <Guid>())) .ReturnsAsync(doesTheUserExist) .Verifiable(); if (doesTheUserExist) { _mockUserService.Setup(Service => Service.CheckIfUserExists(It.IsNotIn <Guid>(new Guid(ConstIds.ExampleUserId)))) .ReturnsAsync(doesTheFriendExist) .Verifiable(); } var controller = new MessagesController(_loggerMock.Object, _mockMessagesService.Object, _mockUserService.Object); //Act var result = await controller.GetMessagesWith(ConstIds.ExampleUserId, ConstIds.ExampleFromWhoId, _paginationParams); //Assert var notFoundObjectResult = Assert.IsType <NotFoundObjectResult>(result.Result); Assert.Equal($"User: {ConstIds.ExampleUserId} or user: {ConstIds.ExampleFromWhoId} doesnt exist.", notFoundObjectResult.Value); _mockUserService.Verify(); }