public async Task PutShouldReturnInvalidModelStateResultWhenInvalidModel() { // Arrange var model = new Flight(); var fakeSql = Substitute.For<IDatabase>(); fakeSql.CreateAsync(model).Returns(Task.FromResult(1)); var controller = new FlightsController(fakeSql) { Configuration = new HttpConfiguration() }; controller.ModelState.AddModelError("FakeError", "This is a Fake Error!"); // Act var result = await controller.Put(1, model); // Assert var viewResult = Assert.IsType<InvalidModelStateResult>(result); Assert.NotNull(viewResult); }
public async Task PutShouldReturnReturnOkResultWithModelDataUpdated() { // Arrange var model = new Flight { Id = 1 }; var datetimeoffset = DateTimeOffset.UtcNow; Mock.Arrange(() => DateTimeOffset.UtcNow).Returns(datetimeoffset); var userid = Guid.NewGuid().ToString(); var fakeContext = Mock.Create<HttpContext>(Constructor.Mocked); Mock.Arrange(() => HttpContext.Current).Returns(fakeContext); Mock.Arrange(() => HttpContext.Current.User.Identity.GetUserId()).Returns(userid); var fakeSql = Substitute.For<IDatabase>(); fakeSql.UpdateAsync(model).Returns(Task.FromResult(true)); var controller = new FlightsController(fakeSql); // Act var result = await controller.Put(2, model); // Assert var viewResult = Assert.IsType<OkNegotiatedContentResult<Flight>>(result); Assert.Equal(viewResult.Content.UserId, userid); Assert.Equal(viewResult.Content.ModifiedDate, datetimeoffset); Assert.Equal(viewResult.Content.Id, 2); }
public async Task PutShouldReturnBadRequestResultWhenResultFalse() { // Arrange var model = new Flight(); var fakeContext = Mock.Create<HttpContext>(Constructor.Mocked); Mock.Arrange(() => HttpContext.Current).Returns(fakeContext); var fakeSql = Substitute.For<IDatabase>(); fakeSql.UpdateAsync(model).Returns(Task.FromResult(false)); var controller = new FlightsController(fakeSql); // Act var result = await controller.Put(1, model); // Assert var viewResult = Assert.IsType<BadRequestResult>(result); Assert.NotNull(viewResult); }