public async Task PostValidModelCallsServiceAndReturnsCreated(EventCreate createModel, int newId) { var service = new Mock <IEventService>(MockBehavior.Strict); var model = createModel.ToItem(); service.Setup(s => s.AddAsync(It.Is <Event>(m => model.Equals(m)))).ReturnsAsync(newId); var controller = GetController(service.Object); var helper = new Mock <IUrlHelper>(MockBehavior.Strict); controller.Url = helper.Object; var result = await controller.Post(createModel); result.Should().NotBeNull().And.BeOfType <CreatedAtActionResult>(); var res = (CreatedAtActionResult)result; res.ControllerName.Should().BeNullOrEmpty(); res.ActionName.Should().Be("Get"); res.RouteValues.Should().NotBeNull().And.Contain(new[] { new KeyValuePair <string, object>("id", newId) }); res.Value.Should().NotBeNull().And.BeOfType <IdResult>().Which.Id.Should().Be(newId); service.VerifyAll(); }
public async Task <IActionResult> Post([FromBody] EventCreate createModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var model = createModel.ToItem(); var id = await Service.AddAsync(model); return(CreatedAtAction("Get", new { id }, new IdResult { Id = id })); }