public async Task GetById_FoodDoesntExist_ShouldReturnNotFound() { Fixture fixture = new Fixture(); var foodRepositoryStub = new Mock <IFoodRepository>(MockBehavior.Strict); foodRepositoryStub.Setup(obj => obj.FindByIdAsync(It.IsAny <int>())).ReturnsAsync((Food)null); FoodsController controller = new FoodsController(foodRepositoryStub.Object); var typedResult = await controller.GetAsync(fixture.Create <int>()) as NotFoundResult; Assert.IsNotNull(typedResult); }
public async Task Get_ShouldReturnAllFoundFoods() { Fixture fixture = new Fixture(); var foods = fixture.CreateMany <Food>(); var foodRepositoryStub = new Mock <IFoodRepository>(MockBehavior.Strict); foodRepositoryStub.Setup(obj => obj.GetAllAsync()).ReturnsAsync(foods); FoodsController controller = new FoodsController(foodRepositoryStub.Object); var result = await controller.GetAsync(); CollectionAssert.AreEqual(foods, result); }
public async Task GetById_FoodExists_ShouldReturnFoundFood() { Fixture fixture = new Fixture(); var foodId = fixture.Create <int>(); var food = fixture.Build <Food>().With(f => f.Id, foodId).Create(); var foodRepositoryStub = new Mock <IFoodRepository>(MockBehavior.Strict); foodRepositoryStub.Setup(obj => obj.FindByIdAsync(foodId)).ReturnsAsync(food); foodRepositoryStub.Setup(obj => obj.SaveChangedAsync()); FoodsController controller = new FoodsController(foodRepositoryStub.Object); var typedResult = await controller.GetAsync(foodId) as OkNegotiatedContentResult <Food>; Assert.IsNotNull(typedResult); Assert.AreSame(food, typedResult.Content); }