public async Task UpdateGroup_ShouldReturnUpdateGroup_WhenGroupExists()
    {
        // Arrange
        const int groupId = 1;

        UpdateGroupBody model = new UpdateGroupBody
        {
            Name        = "Some updated name",
            Description = "Some updated description"
        };

        Mock <IMediator> mediatorMock = new Mock <IMediator>();

        mediatorMock
        .Setup(m => m.Send(It.IsAny <GroupExistsQuery>(), It.IsAny <CancellationToken>()))
        .ReturnsAsync(true);

        GroupController controller = new GroupController(mediatorMock.Object, null);

        // Act
        ActionResult response = await controller.UpdateGroup(groupId, model);

        // Assert
        Assert.IsType <NoContentResult>(response);

        mediatorMock.Verify(m => m.Send(It.IsAny <UpdateGroupCommand>(), It.IsAny <CancellationToken>()));
    }
Esempio n. 2
0
        public async Task <ActionResult> UpdateGroup([FromRoute] int groupId, [FromBody] UpdateGroupBody model, CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            GroupExistsQuery existsQuery = new GroupExistsQuery {
                GroupId = groupId
            };

            bool exists = await _mediator.Send(existsQuery, cancellationToken);

            if (!exists)
            {
                return(NotFound(new ErrorResource
                {
                    StatusCode = StatusCodes.Status404NotFound,
                    Message = $"Group with ID '{groupId}' does not exist"
                }));
            }

            UpdateGroupCommand updateCommand = new UpdateGroupCommand
            {
                GroupId     = groupId,
                Name        = model.Name,
                Description = model.Description
            };

            await _mediator.Send(updateCommand, cancellationToken);

            return(NoContent());
        }
    public async Task UpdateGroup_ShouldReturnNotFoundResult_WhenGroupDoesNotExist()
    {
        // Arrange
        const int groupId = 15453;

        UpdateGroupBody model = new UpdateGroupBody
        {
            Name        = "Some updated name",
            Description = "Some updated description"
        };

        Mock <IMediator> mediatorMock = new Mock <IMediator>();

        mediatorMock
        .Setup(m => m.Send(It.IsAny <GroupExistsQuery>(), It.IsAny <CancellationToken>()))
        .ReturnsAsync(false);

        GroupController controller = new GroupController(mediatorMock.Object, null);

        // Act
        ActionResult response = await controller.UpdateGroup(groupId, model);

        // Assert
        NotFoundObjectResult result = Assert.IsType <NotFoundObjectResult>(response);

        ErrorResource error = Assert.IsType <ErrorResource>(result.Value);

        Assert.Equal(StatusCodes.Status404NotFound, error.StatusCode);
    }