Exemplo n.º 1
0
        public void Create_ReturnsBadRequest_WhenEmptyKey()
        {
            var key = string.Empty;

            var result = _commentController.Create(key);

            result.Should().BeAssignableTo <BadRequestResult>();
        }
Exemplo n.º 2
0
 GivenACommentController_WhenCreateIsCalled_AndTheCommentIsInvalid_ThenTheCommentStateIsAddedToTempData()
 {
     _controller.ModelState.AddModelError("Name", "Name error");
     _controller.Create(new AddCommentViewModel(1, true)
     {
         Name = Name, Comment = Comment
     });
     Assert.That(_controller.TempData, Is.Not.Null);
     Assert.That(_controller.TempData["comment"], Is.Not.Null);
 }
Exemplo n.º 3
0
        public void Add_Comment()
        {
            //Act
            Comments testComment        = new Comments("TestComment", "Comment to add for testing", m_ParentTaskID);
            int      initalCommentCount = m_ScrumToolDBContext.Comments.Count();

            //Arrange
            m_CommentController.Create(testComment);
            //Assert
            m_ScrumToolDBContext.Comments.Should().NotBeNullOrEmpty()
            .And.HaveCount(initalCommentCount + 1, "Inital Comment Count + 1");
            m_ScrumToolDBContext.Comments.Last().ShouldBeEquivalentTo(testComment,
                                                                      options => options.Excluding(c => c.ID), "The last comment object in the table should be the testComment");
        }
Exemplo n.º 4
0
        public async Task Create_ShouldReturn_BadRequest_ForInvalidModel()
        {
            var mock       = new Mock <ICommentRepository>();
            var controller = new CommentController(mock.Object);

            controller.ModelState.AddModelError("error", "erro occured");

            var actionResult = await controller.Create(null);

            Assert.IsNotNull(actionResult);

            BadRequestResult result = actionResult as BadRequestResult;

            Assert.IsNotNull(result);
        }
        public void ShouldCreateCommentWithBadRequestError()
        {
            RequestResult <UserComment> expectedResult = new RequestResult <UserComment>()
            {
                ResourcePayload = null, // empty comment
                ResultStatus    = Common.Constants.ResultType.Success,
            };

            Mock <IHttpContextAccessor> httpContextAccessorMock = CreateValidHttpContext(Token, UserId, Hdid);
            Mock <ICommentService>      commentServiceMock      = new Mock <ICommentService>();

            commentServiceMock.Setup(s => s.Add(expectedResult.ResourcePayload)).Returns(expectedResult);

            CommentController service = new CommentController(commentServiceMock.Object, httpContextAccessorMock.Object);
            var actualResult          = service.Create(Hdid, expectedResult.ResourcePayload);

            Assert.IsType <BadRequestResult>(actualResult);
        }
        public void Create_adds_new_comment_to_given_ticket()
        {
            // Arrange
            var model = _fixture.Create <CreateCommentModel>();

            var ticket = _fixture.Create <Ticket>();

            _ticketFinderMock.Setup(f => f.Find(model.TicketId))
            .Returns(ticket);

            // Act
            var result = (RedirectToActionResult)_controller.Create(model);

            ticket.GetComments().Single().Should().BeEquivalentTo(model.ToDomainObject());
            _ticketModifierMock.Verify(m => m.UpdateTicket(ticket), Times.Once);

            result.ActionName.Should().Be(nameof(TicketsController.Details));
            result.ControllerName.Should().Be("Tickets");
            result.RouteValues["Id"].Should().Be(model.TicketId);
        }
Exemplo n.º 7
0
        public async void Create_Errors()
        {
            CommentControllerMockFacade mock = new CommentControllerMockFacade();

            var mockResponse = new Mock <CreateResponse <ApiCommentServerResponseModel> >(null as ApiCommentServerResponseModel);
            var mockRecord   = new ApiCommentServerResponseModel();

            mockResponse.SetupGet(x => x.Success).Returns(false);

            mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiCommentServerRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiCommentServerResponseModel> >(mockResponse.Object));
            CommentController controller = new CommentController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object);

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            IActionResult response = await controller.Create(new ApiCommentServerRequestModel());

            response.Should().BeOfType <ObjectResult>();
            (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity);
            mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiCommentServerRequestModel>()));
        }
Exemplo n.º 8
0
        public void CommentTestOpen()
        {
            Post openPost = new Post {
                title = "test", Text = "tester", ID = 1, CommentsAllowed = true
            };
            Comment k = new Comment {
                ID = 1, Title = "test", Text = "test"
            };

            repos.Setup(x => x.GetPostWithComments(1)).Returns(openPost);
            repos.Setup(x => x.CreateComment(k, 3)).Returns(true);

            var controller = new CommentController(repos.Object);

            var result = controller.Create(k, k.ID);

            repos.Verify(x => x.CreateComment(k, k.PostID));

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }
Exemplo n.º 9
0
        public void CommentTestClosed()
        {
            Post closedPost = new Post {
                title = "test", Text = "tester", ID = 44, CommentsAllowed = false, BlogID = 1
            };
            Comment comment = new Comment {
                ID = 2, Title = "test", Text = "test", PostID = 44
            };

            repos.Setup(x => x.GetPostWithComments(1)).Returns(closedPost);
            repos.Setup(x => x.CreateComment(comment, 3)).Returns(true);

            var controller = new CommentController(repos.Object);

            var result = controller.Create(comment, comment.ID);

            repos.Verify(x => x.CreateComment(comment, comment.PostID));

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }
Exemplo n.º 10
0
        public void ShouldCreateComment()
        {
            RequestResult <UserComment> expectedResult = new RequestResult <UserComment>()
            {
                ResourcePayload = new UserComment()
                {
                    Id = Guid.NewGuid(),
                },
                ResultStatus = Common.Constants.ResultType.Success,
            };

            Mock <IHttpContextAccessor> httpContextAccessorMock = CreateValidHttpContext(Token, UserId, Hdid);
            Mock <ICommentService>      commentServiceMock      = new Mock <ICommentService>();

            commentServiceMock.Setup(s => s.Add(expectedResult.ResourcePayload)).Returns(expectedResult);

            CommentController service = new CommentController(commentServiceMock.Object, httpContextAccessorMock.Object);
            var actualResult          = service.Create(Hdid, expectedResult.ResourcePayload);

            Assert.True(((JsonResult)actualResult).Value.IsDeepEqual(expectedResult));
        }
Exemplo n.º 11
0
        public void CheckIfCreateMethodReturnsRedirectWhenOk()
        {
            //Arrange
            var postService    = MockCreator.PostServiceMock();
            var commentService = MockCreator.CommentServiceMock();

            var controller = new CommentController(postService.Object, commentService.Object);

            var model = new PostCommentCreateModel
            {
                CommentText = "Test"
            };

            //Act
            var result = controller.Create(model, null);

            //Assert
            result
            .Should()
            .BeOfType <RedirectToActionResult>();
        }
Exemplo n.º 12
0
        public void CheckIfCreateMethodReturnsViewWithErrorWhenModelTextIsEmpty()
        {
            //Arrange
            var postService    = MockCreator.PostServiceMock();
            var commentService = MockCreator.CommentServiceMock();

            var controller = new CommentController(postService.Object, commentService.Object);

            var model = new PostCommentCreateModel
            {
                CommentText = ""
            };

            //Act
            var result = controller.Create(model, null);

            //Assert
            result
            .Should()
            .BeOfType <ViewResult>();
        }
Exemplo n.º 13
0
        public async void Create_No_Errors()
        {
            CommentControllerMockFacade mock = new CommentControllerMockFacade();

            var mockResponse = ValidationResponseFactory <ApiCommentServerResponseModel> .CreateResponse(null as ApiCommentServerResponseModel);

            mockResponse.SetRecord(new ApiCommentServerResponseModel());
            mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiCommentServerRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiCommentServerResponseModel> >(mockResponse));
            CommentController controller = new CommentController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object);

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            IActionResult response = await controller.Create(new ApiCommentServerRequestModel());

            response.Should().BeOfType <CreatedResult>();
            (response as CreatedResult).StatusCode.Should().Be((int)HttpStatusCode.Created);
            var createResponse = (response as CreatedResult).Value as CreateResponse <ApiCommentServerResponseModel>;

            createResponse.Record.Should().NotBeNull();
            mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiCommentServerRequestModel>()));
        }
Exemplo n.º 14
0
        public async Task Create_ShouldReturn_CreatedComment()
        {
            const string TEXT = "Unit Test comments goes here...";

            #region Arrange
            int id          = 1;
            var testComment = new Comment()
            {
                Id         = 1,
                Text       = TEXT,
                PostId     = 1,
                InsertDate = DateTime.Now,
            };

            var mockRepo = new Mock <ICommentRepository>();
            mockRepo.Setup(repo => repo.Find(id))
            .Returns(Task.FromResult(testComment));
            var controller = new CommentController(mockRepo.Object);

            var newComment = new AddOrUpdateCommentDTO()
            {
                Text   = TEXT,
                PostId = 1,
            };
            mockRepo.Setup(repo => repo.Add(testComment))
            .Returns(Task.CompletedTask)
            .Verifiable();
            #endregion


            var actionResult = await controller.Create(newComment);

            Assert.IsInstanceOfType(actionResult, typeof(CreatedAtRouteResult));

            var okResult = actionResult as CreatedAtRouteResult;
            Assert.IsInstanceOfType(okResult.Value, typeof(Comment));
            Assert.AreEqual(TEXT, (okResult.Value as Comment).Text);
        }
        public void TestAddComment()
        {
            var identity = new GenericIdentity("*****@*****.**");

            Thread.CurrentPrincipal = new GenericPrincipal(identity, null);

            var commentResult = (controller.GetComments(1) as PartialViewResult);

            Assert.IsNotNull(commentResult);

            var comments = commentResult.Model as IEnumerable <Comment>;

            Assert.IsNotNull(comments);

            var commentCount = comments.Count();

            var createCommentResult = controller.Create(new Comment
            {
                Text      = "Hello world!",
                TimeStamp = DateTime.Now,
                User      = new User {
                    Id = 1, Email = "*****@*****.**"
                }
            }, 1);

            Assert.IsNotNull(createCommentResult);

            commentResult = (controller.GetComments(1) as PartialViewResult);
            comments      = commentResult?.Model as IEnumerable <Comment>;

            Assert.IsNotNull(comments);

            var newCommentCount = comments.Count();

            Assert.AreEqual(commentCount + 1, newCommentCount);
        }