Esempio n. 1
0
        public async Task <ActionResult <GroupResource> > CreateGroup([FromBody] CreateGroupBody model, CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            CreateGroupCommand command = _mapper.Map <CreateGroupBody, CreateGroupCommand>(model);

            GroupResource group = await _mediator.Send(command, cancellationToken);

            return(CreatedAtAction(nameof(GetGroupById), new { groupId = group.GroupId }, group));
        }
    public async Task CreateGroup_ShouldReturnCreatedResult_WhenGroupIsCreated()
    {
        // Arrange
        CreateGroupBody model = new CreateGroupBody
        {
            Name        = "Some group name",
            Description = "Some group description"
        };

        GroupResource expectedGroup = new GroupResource
        {
            GroupId     = 1,
            Name        = model.Name,
            Description = model.Description
        };

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

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

        MapperConfiguration mapperConfiguration = new MapperConfiguration(config =>
        {
            config.CreateMap <CreateGroupBody, CreateGroupCommand>();
        });

        IMapper mapperMock = mapperConfiguration.CreateMapper();

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

        // Act
        ActionResult <GroupResource> response = await controller.CreateGroup(model);

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

        GroupResource actualGroup = Assert.IsType <GroupResource>(result.Value);

        Assert.NotNull(actualGroup);
        Assert.Equal(1, actualGroup.GroupId);

        mediatorMock.Verify(m => m.Send(It.IsAny <CreateGroupCommand>(), It.IsAny <CancellationToken>()));
    }