Пример #1
0
        public async Task GivenValidRequest_WhenTheArticleDoesNotExist_ThrowsApiException()
        {
            // Arrange
            var commentDto = new AddCommentDto
            {
                Body = "This article sucks!"
            };
            var addCommentCommand = new AddCommentCommand
            {
                Slug    = "how-to-not-train-your-dragon",
                Comment = commentDto
            };

            // Act
            var handler  = new AddCommentCommandHandler(CurrentUserContext, Context, Mapper, MachineDateTime);
            var response = await Should.ThrowAsync <ConduitApiException>(async() =>
            {
                await handler.Handle(addCommentCommand, CancellationToken.None);
            });

            // Assert
            response.StatusCode.ShouldBe(HttpStatusCode.NotFound);
            response.ShouldNotBeNull();
            response.ShouldBeOfType <ConduitApiException>();
        }
Пример #2
0
        public async Task GivenValidRequest_WhenTheArticleExists_AddsCommentToArticle()
        {
            // Arrange
            var commentDto = new AddCommentDto
            {
                Body = "This article sucks!"
            };
            var addCommentCommand = new AddCommentCommand
            {
                Slug    = "how-to-train-your-dragon",
                Comment = commentDto
            };
            var articleComments = Context.Articles
                                  .Include(a => a.Comments)
                                  .FirstOrDefault(a => a.Slug == "how-to-train-your-dragon")?
                                  .Comments;

            articleComments?.Count.ShouldBe(2);
            articleComments?.ShouldNotContain(c => c.Body == "This article sucks!");

            // Act
            var handler  = new AddCommentCommandHandler(CurrentUserContext, Context, Mapper, MachineDateTime);
            var response = await handler.Handle(addCommentCommand, CancellationToken.None);

            // Assert
            response.ShouldNotBeNull();
            response.ShouldBeOfType <CommentViewModel>();
            response.Comment.ShouldNotBeNull();
            response.Comment.ShouldBeOfType <CommentDto>();
            articleComments?.Count.ShouldBe(3);
            articleComments?.ShouldContain(c => c.Body == "This article sucks!");
        }
Пример #3
0
        public async Task AddCommentCommand_ShouldAddComment()
        {
            Assert.AreEqual(0, _invitation.Comments.Count);

            await _dut.Handle(_command, default);

            Assert.AreEqual(1, _invitation.Comments.Count);
            _unitOfWorkMock.Verify(t => t.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Once);
        }
Пример #4
0
        public void Should_call_validate_command_when_add_new_comment()
        {
            var comment = Comment.CreateNew(Guid.NewGuid(), "Text", false);
            var command = new AddCommentCommand(comment.PostId, comment.Text, comment.Approved);

            var commentRepository = new Mock <ICommentRepository>();

            var validator = new Mock <IValidator <AddCommentCommand> >();

            validator.Setup(x => x.Validate(command)).Returns(new ValidationResult());

            var addCommentCommandHandler = new AddCommentCommandHandler(commentRepository.Object, validator.Object);

            addCommentCommandHandler.Handle(command);

            validator.Verify(x => x.Validate(command));
        }
Пример #5
0
        public async Task Should_ThrowException_When_AddingComment_If_ParticipantIsNot()
        {
            var mockIdentityService = new Mock <IIdentityService>();

            mockIdentityService.Setup((x) => x.GetUserId()).Returns("456");

            var mockMeetupPolicy = new Mock <IMeetupPolicy>();

            mockMeetupPolicy.Setup((x) => x.CheckCanDefineMeetup(It.IsAny <string>(), It.IsAny <DateTime>())).Callback(() => { });

            var meetup = new Meetup(
                organizerId: "123",
                subject: "DDD",
                when: DateTime.Now.AddDays(1),
                description: "DDD Practices",
                location: new Location("YTÜ Teknopark"),
                policy: mockMeetupPolicy.Object);

            meetup.Complete();

            _fixture.dbContext.Add(meetup);

            var meetupRepository = new MeetupRepository(_fixture.dbContext);

            var addMeetupCommentCommandHandler = new AddCommentCommandHandler(meetupRepository, mockIdentityService.Object);
            var cancellationToken = new CancellationToken();

            var addMeetupCommentCommand = new AddCommentCommand
            {
                MeetupId = meetup.Id,
                Comment  = "Good!"
            };

            var exception = await Assert.ThrowsAsync <MeetupDomainException>(async() =>
            {
                await addMeetupCommentCommandHandler.Handle(addMeetupCommentCommand, cancellationToken);
            });

            Assert.Equal("You are not a participant", exception.Message);
        }
Пример #6
0
        public async Task Should_Sucess_When_AddingComment()
        {
            var mockIdentityService = new Mock <IIdentityService>();

            mockIdentityService.Setup((x) => x.GetUserId()).Returns("456");

            var mockMeetupPolicy = new Mock <IMeetupPolicy>();

            mockMeetupPolicy.Setup((x) => x.CheckCanDefineMeetup(It.IsAny <string>(), It.IsAny <DateTime>())).Callback(() => { });

            var meetup = new Meetup(
                organizerId: "123",
                subject: "DDD",
                when: DateTime.Now.AddDays(1),
                description: "DDD Practices",
                location: new Location("YTÜ Teknopark"),
                policy: mockMeetupPolicy.Object);

            meetup.Join("456");
            meetup.Complete();

            _fixture.dbContext.Add(meetup);

            var meetupRepository = new MeetupRepository(_fixture.dbContext);

            var addMeetupCommentCommandHandler = new AddCommentCommandHandler(meetupRepository, mockIdentityService.Object);
            var cancellationToken = new CancellationToken();

            var addMeetupCommentCommand = new AddCommentCommand
            {
                MeetupId = meetup.Id,
                Comment  = "Good!"
            };

            await addMeetupCommentCommandHandler.Handle(addMeetupCommentCommand, cancellationToken);

            Assert.Single(_fixture.dbContext.Meetups.FirstOrDefault().Comments);
        }