Esempio n. 1
0
        public async Task <IActionResult> Update(int id, [FromBody] SuperPowerModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var superPower = await _superPowerService.GetByIdAsync(id);

                superPower.Name = model.Name;

                model = _mapper.Map <SuperPowerModel>(await _superPowerService.UpdateAsync(superPower));
                return(Ok(model));
            }
            catch (CustomNotFoundException)
            {
                return(NotFound());
            }
            catch (CustomException ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Esempio n. 2
0
        public async void Create_ShouldReturnCreatedAtActionResult()
        {
            var expectedId    = 1;
            var expectedModel = new SuperPowerModel {
                Name = "power"
            };
            var superPower = _mapper.Map <SuperPower>(expectedModel);

            var expectedActionName = nameof(_controllerTest.GetById);

            _superPowerServiceMock.Setup(t => t.InsertAsync(It.IsAny <SuperPower>()))
            .ReturnsAsync(() =>
            {
                superPower.Id = expectedId;
                return(superPower);
            });


            var result = await _controllerTest.Create(expectedModel);


            var createdResult = Assert.IsType <CreatedAtActionResult>(result);

            Assert.Equal(expectedId, ((SuperPowerModel)createdResult.Value).Id);
            Assert.Equal(expectedActionName, createdResult.ActionName);

            _superPowerServiceMock.Verify(t => t.InsertAsync(It.IsAny <SuperPower>()), Times.Once);
        }
Esempio n. 3
0
        public async void Create_ShouldReturnBadRequestCustomFieldAlreadyExistsException()
        {
            var expected = new SuperPowerModel();

            _superPowerServiceMock.Setup(t => t.InsertAsync(It.IsAny <SuperPower>()))
            .ThrowsAsync(new CustomFieldAlreadyExistsException(""));

            var result = await _controllerTest.Create(expected);

            Assert.IsType <BadRequestObjectResult>(result);
        }
Esempio n. 4
0
        public async void Update_ShouldReturnBadRequest()
        {
            var obj = new SuperPowerModel();

            _controllerTest.ModelState.AddModelError("k", "v");

            var result = await _controllerTest.Update(1, obj);

            var badRequestResult = Assert.IsType <BadRequestObjectResult>(result);

            Assert.IsType <SerializableError>(badRequestResult.Value);
            _superPowerServiceMock.Verify(t => t.GetByIdAsync(It.IsAny <int>()), Times.Never);
            _superPowerServiceMock.Verify(t => t.UpdateAsync(It.IsAny <SuperPower>()), Times.Never);
        }
Esempio n. 5
0
        public async void Update_ShouldReturnNotFound()
        {
            var model      = new SuperPowerModel();
            var superPower = new SuperPower();


            _superPowerServiceMock.Setup(t => t.GetByIdAsync(It.IsAny <int>()))
            .ThrowsAsync(new CustomNotFoundException("Super Power"));

            var result = await _controllerTest.Update(1, model);

            Assert.IsType <NotFoundResult>(result);
            _superPowerServiceMock.Verify(t => t.GetByIdAsync(It.IsAny <int>()), Times.Once);
            _superPowerServiceMock.Verify(t => t.UpdateAsync(It.IsAny <SuperPower>()), Times.Never);
        }
Esempio n. 6
0
        public async void Create_ShouldReturnBadRequest()
        {
            var expected = new SuperPowerModel {
                Name = "power"
            };

            _controllerTest.ModelState.AddModelError("k", "v");

            var result = await _controllerTest.Create(expected);

            var badRequestResult = Assert.IsType <BadRequestObjectResult>(result);

            Assert.IsType <SerializableError>(badRequestResult.Value);
            _superPowerServiceMock.Verify(t => t.InsertAsync(It.IsAny <SuperPower>()), Times.Never);
        }
Esempio n. 7
0
        public async Task <IActionResult> Create([FromBody] SuperPowerModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var superPower = _mapper.Map <SuperPower>(model);
                var created    = await _superPowerService.InsertAsync(superPower);

                model = _mapper.Map <SuperPowerModel>(created);
                return(CreatedAtAction(nameof(GetById), new { id = created.Id }, model));
            }
            catch (CustomException ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Esempio n. 8
0
        public async void Update_ShouldReturnOkObjectResult()
        {
            var expectedModel = new SuperPowerModel {
                Name = "power"
            };
            SuperPower superPower = _mapper.Map <SuperPower>(expectedModel);

            _superPowerServiceMock.Setup(t => t.GetByIdAsync(1))
            .ReturnsAsync(superPower)
            .Verifiable();

            _superPowerServiceMock.Setup(t => t.UpdateAsync(superPower))
            .ReturnsAsync(superPower)
            .Verifiable();

            var result = await _controllerTest.Update(1, expectedModel);

            var objectResult = Assert.IsType <OkObjectResult>(result).Value as SuperPowerModel;

            Assert.Equal(expectedModel.Id, objectResult.Id);
            _superPowerServiceMock.Verify(t => t.GetByIdAsync(It.IsAny <int>()), Times.Once);
            _superPowerServiceMock.Verify(t => t.UpdateAsync(It.IsAny <SuperPower>()), Times.Once);
        }