public void GetOrdersOfRestaurantTest() { OrdersOfRestaurantController ordersController = CreateFakeOrdersOfRestaurantController(); //Retrieving all orders of existing restaurant var response = ordersController.GetAllOrdersOfRestaurant(_restaurants[0].Id); Assert.IsType <OkObjectResult>(response.Result); Assert.Equal(_orders.FindAll(o => o.RestaurantId == _restaurants[0].Id).Count, ((IEnumerable <GetOrderModel>)((OkObjectResult)response.Result).Value).Count()); //Retrieving all orders of non-existing restaurant response = ordersController.GetAllOrdersOfRestaurant(0); Assert.IsType <NotFoundObjectResult>(response.Result); }
private OrdersOfRestaurantController CreateFakeOrdersOfRestaurantController(User loggedUser = null) { //Create fake DBContext var context = new GlovoDbContext(ContextOptions); //Create fake HttpContextAccessor var httpContext = new DefaultHttpContext(); var httpContextAccessor = new HttpContextAccessor { HttpContext = httpContext }; //Add logged user to HttpContextAccessor in case it is needed if (loggedUser != null) { httpContextAccessor.HttpContext.Items["User"] = loggedUser; } //Create RestApiRestaurantsService instance with fake DBContext and HttpContextAccessor _ordersService = new RestApiOrdersService(context, httpContextAccessor); //Create mapper with UsersProfile var mapper = new MapperConfiguration(cfg => { cfg.AddProfile <LocationsProfile>(); cfg.AddProfile <OrdersProductsProfile>(); cfg.AddProfile <OrdersProfile>(); cfg.AddProfile <ProductsProfile>(); cfg.AddProfile <RestaurantsProfile>(); cfg.AddProfile <UsersProfile>(); }).CreateMapper(); //Create UsersController instance with the RestApiRestaurantsService instance and the mapper var ordersController = new OrdersOfRestaurantController(_ordersService, mapper) { ControllerContext = { HttpContext = httpContext } }; return(ordersController); }