public async Task Post_CreationSucceeds_ShouldReturnCreatedFoodLocation() { Fixture fixture = new Fixture(); var food = fixture.Create <Food>(); var foodRepositoryStub = new Mock <IFoodRepository>(MockBehavior.Strict); foodRepositoryStub.SetupSequence(obj => obj.FindByDescriptionAsync(It.IsAny <string>())) .ReturnsAsync(null) .ReturnsAsync(food); foodRepositoryStub.Setup(obj => obj.Create(food)); foodRepositoryStub.Setup(obj => obj.SaveChangedAsync()).Returns(Task.CompletedTask); var locationUrl = "http://location/"; var urlHelperStub = new Mock <UrlHelper>(MockBehavior.Strict); urlHelperStub.Setup(obj => obj.Link(It.IsAny <string>(), It.IsAny <object>())).Returns(locationUrl); FoodsController controller = new FoodsController(foodRepositoryStub.Object); controller.Request = new HttpRequestMessage(); controller.Configuration = new HttpConfiguration(); controller.Url = urlHelperStub.Object; var typedResult = await controller.PostAsync(food) as CreatedNegotiatedContentResult <Food>; Assert.AreSame(locationUrl, typedResult.Location.AbsoluteUri); }
public async Task Post_CreationSucceeds_ShouldReturnCreatedFood() { Fixture fixture = new Fixture(); var food = fixture.Create <Food>(); var foodRepositoryStub = new Mock <IFoodRepository>(MockBehavior.Strict); foodRepositoryStub.SetupSequence(obj => obj.FindByDescriptionAsync(It.IsAny <string>())) .ReturnsAsync(null) .ReturnsAsync(food); foodRepositoryStub.Setup(obj => obj.Create(food)); foodRepositoryStub.Setup(obj => obj.SaveChangedAsync()).Returns(Task.CompletedTask); var urlHelperStub = new Mock <UrlHelper>(); urlHelperStub.Setup(obj => obj.Link(It.IsAny <string>(), It.IsAny <object>())).Returns(string.Empty); FoodsController controller = new FoodsController(foodRepositoryStub.Object); controller.Url = urlHelperStub.Object; var typedResult = await controller.PostAsync(food) as CreatedNegotiatedContentResult <Food>; Assert.IsNotNull(typedResult); Assert.AreSame(food, typedResult.Content); }
public async Task Post_CreatingDuplicate_ShouldReturnConflict() { Fixture fixture = new Fixture(); var foodRepositoryStub = new Mock <IFoodRepository>(MockBehavior.Strict); foodRepositoryStub.Setup(obj => obj.FindByDescriptionAsync(It.IsAny <string>())).ReturnsAsync(fixture.Create <Food>()); FoodsController controller = new FoodsController(foodRepositoryStub.Object); var typedResult = await controller.PostAsync(fixture.Create <Food>()) as ConflictResult; Assert.IsNotNull(typedResult); }
public async Task Post_CreationFails_ShouldReturnBadRequest() { Fixture fixture = new Fixture(); var foodRepositoryStub = new Mock <IFoodRepository>(MockBehavior.Strict); foodRepositoryStub.Setup(obj => obj.FindByDescriptionAsync(It.IsAny <string>())).ReturnsAsync((Food)null); foodRepositoryStub.Setup(obj => obj.Create(It.IsAny <Food>())); foodRepositoryStub.Setup(obj => obj.SaveChangedAsync()).Throws <RepositoryException>(); FoodsController controller = new FoodsController(foodRepositoryStub.Object); var typedResult = await controller.PostAsync(fixture.Create <Food>()) as BadRequestResult; Assert.IsNotNull(typedResult); }