public async Task EditAsync_WithCorrectData_ShouldSuccessfullyEdit()
        {
            var testContent = "TestContent";

            // Arrange
            var context           = ApplicationDbContextInMemoryFactory.InitializeContext();
            var commentRepository = new EfDeletableEntityRepository <Comment>(context);
            var commentsService   = new CommentsService(commentRepository);

            var inputModel = new CreateCommentInputModel()
            {
                CommentContent = testContent,
            };

            await commentsService.CreateAsync(inputModel);

            var comment = commentRepository.All().FirstOrDefault(c => c.Content == testContent);

            // Act
            var expectedCommentContent = "Edited_TestContent";
            await commentsService.EditAsync(comment.Id, expectedCommentContent);

            var actualCommentsContent = comment.Content;

            // Assert
            comment = await commentRepository.GetByIdWithDeletedAsync(comment.Id);

            Assert.Equal(expectedCommentContent, actualCommentsContent);
        }
        public async Task EditAsyncEditsThePropertiesAndSavesTheChanges(
            string userId,
            string postId,
            string text,
            string newText)
        {
            // Arrange
            AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly);

            var saved   = false;
            var comment = new Comment()
            {
                ApplicationUserId = userId,
                Text   = text,
                PostId = postId,
            };

            var commentList = new List <Comment>()
            {
                new Comment(),
                comment,
                new Comment(),
                new Comment(),
            };

            var repositoryMock = new Mock <IDeletableEntityRepository <Comment> >();

            repositoryMock.Setup(x => x.All())
            .Returns(commentList.AsQueryable());

            repositoryMock.Setup(x => x.SaveChangesAsync())
            .Callback(() =>
            {
                saved = true;
            });

            var commentsService = new CommentsService(repositoryMock.Object, AutoMapperConfig.MapperInstance);

            var commentEditModel = new CommentInputModel()
            {
                Text   = newText,
                PostId = postId,
            };

            // Act
            await commentsService.EditAsync(comment.Id, commentEditModel);

            // Assert
            Assert.True(saved);

            Assert.Equal(newText, comment.Text);
            Assert.Equal(postId, comment.PostId);
            Assert.Equal(userId, comment.ApplicationUserId);
        }
        public async Task EditAsyncThrowsWhenTheInputModelIsNull()
        {
            // Arrange
            var mapperMock     = new Mock <IMapper>();
            var repositoryMock = new Mock <IDeletableEntityRepository <Comment> >();

            var commentsService = new CommentsService(repositoryMock.Object, mapperMock.Object);

            // Assert
            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                // Act
                await commentsService.EditAsync("validId", null);
            });
        }
        public async Task EditAsync_WithIncorrectData_ShouldThrowArgumentNullException()
        {
            var incorrectId = Guid.NewGuid().ToString();

            // Arrange
            var context           = ApplicationDbContextInMemoryFactory.InitializeContext();
            var commentRepository = new EfDeletableEntityRepository <Comment>(context);
            var commentsService   = new CommentsService(commentRepository);

            // Act

            // Assert
            await Assert.ThrowsAsync <ArgumentException>(async() =>
            {
                await commentsService.EditAsync(incorrectId, "RandomContent");
            });
        }
示例#5
0
        public async Task <IActionResult> Edit(int id, CommentCreateViewModel model)
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            if (await _commentsService.CanEditAsync(userId, id))
            {
                if (ModelState.IsValid)
                {
                    ViewBag.Id = id;
                    await _commentsService.EditAsync(id, model);

                    return(RedirectToAction("Index", "Post"));
                }
                return(View(model));
            }

            return(RedirectToAction("Index", "Post"));
        }