Exemplo n.º 1
0
        public async Task Should_succeed_with_status_deleted_when_delete_is_valid()
        {
            // Arrange
            var message = MockedMessageBuilder
                          .SetId(1)
                          .SetMessage("The message")
                          .SetClientId(Guid.NewGuid().ToString())
                          .Build()
                          .Object;

            _mockedRepository.Setup(x => x.GetAsync(It.IsAny <int>())).ReturnsAsync(message);
            _mockedRepository.Setup(x => x.UnitOfWork).Returns(_mockedUnitOfWork.Object);
            _mockedUnitOfWork.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).ReturnsAsync(1);

            var command = new DeleteMessageCommand(message.Id, message.ClientId);
            var handler = new DeleteMessageCommandHandler(_mockedRepository.Object);

            // Act
            var result = await handler.Handle(command, default);

            // Assert
            Assert.True(result.Succeeded);
            Assert.IsType <Deleted>(result.StatusCode);

            _mockedRepository.Verify(x => x.GetAsync(command.MessageId), Times.Once);
            _mockedRepository.Verify(x => x.Remove(message), Times.Once);
            _mockedUnitOfWork.Verify(x => x.SaveChangesAsync(default), Times.Once);
        public async Task Should_return_message_when_specified_id_exists()
        {
            // Arrange
            var message = MockedMessageBuilder
                          .SetId(1)
                          .SetMessage("The message")
                          .SetClientId(Guid.NewGuid().ToString())
                          .Build().Object;

            _mockedReadOnlyContext
            .Setup(x => x.FindAsync <BoardMessage>(It.IsAny <int>()))
            .ReturnsAsync(() => message);

            var query   = new GetMessageQuery(message.Id);
            var handler = new GetMessageQueryHandler(_mockedReadOnlyContext.Object);

            // Act
            var messageDto = await handler.Handle(query, default);

            // Assert
            Assert.NotNull(messageDto);
            _mockedReadOnlyContext.Verify(x => x.FindAsync <BoardMessage>(message.Id), Times.Once);
        }
Exemplo n.º 3
0
        public async Task Should_fail_with_status_bad_request_when_attempting_to_delete_message_created_by_another_client()
        {
            // Arrange
            var message = MockedMessageBuilder
                          .SetId(1)
                          .SetMessage("The message")
                          .SetClientId(Guid.NewGuid().ToString())
                          .Build()
                          .Object;

            _mockedRepository.Setup(x => x.GetAsync(It.IsAny <int>())).ReturnsAsync(message);

            var command = new DeleteMessageCommand(message.Id, Guid.NewGuid().ToString());
            var handler = new DeleteMessageCommandHandler(_mockedRepository.Object);

            // Act
            var result = await handler.Handle(command, default);

            // Assert
            Assert.True(result.Failed);
            Assert.IsType <BadRequest>(result.StatusCode);

            _mockedRepository.Verify(x => x.GetAsync(command.MessageId), Times.Once);
        }