private RemoveFromCart ( int id, |
||
id | int | |
requestAborted | ||
Résultat | Task |
public async Task RemoveFromCart_RemovesItemFromCart() { // Arrange var cartId = "CartId_A"; var cartItemId = 3; var numberOfItem = 5; var unitPrice = 10; var httpContext = new DefaultHttpContext(); // Session and cart initialization httpContext.Session = new TestSession(); httpContext.Session.SetString("Session", cartId); // DbContext initialization var dbContext = _fixture.Context; var cartItems = CreateTestCartItems(cartId, unitPrice, numberOfItem); dbContext.AddRange(cartItems.Select(n => n.Album).Distinct()); dbContext.AddRange(cartItems); dbContext.SaveChanges(); // ServiceProvder initialization var serviceProviderFeature = new ServiceProvidersFeature(); httpContext.Features.Set <IServiceProvidersFeature>(serviceProviderFeature); // AntiForgery initialization serviceProviderFeature.RequestServices = _fixture.ServiceProvider; var antiForgery = serviceProviderFeature.RequestServices.GetRequiredService <IAntiforgery>(); var tokens = antiForgery.GetTokens(httpContext); // Header initialization for AntiForgery var headers = new KeyValuePair <string, StringValues>( "RequestVerificationToken", new string[] { tokens.CookieToken + ":" + tokens.RequestToken }); httpContext.Request.Headers.Add(headers); // Cotroller initialization var controller = new ShoppingCartController( dbContext, _fixture.ServiceProvider.GetService <ILogger <ShoppingCartController> >()); controller.ControllerContext.HttpContext = httpContext; // Act var result = await controller.RemoveFromCart(cartItemId, CancellationToken.None); // Assert var jsonResult = Assert.IsType <JsonResult>(result); var viewModel = Assert.IsType <ShoppingCartRemoveViewModel>(jsonResult.Value); Assert.Equal(numberOfItem - 1, viewModel.CartCount); Assert.Equal((numberOfItem - 1) * 10, viewModel.CartTotal); Assert.Equal("Greatest Hits has been removed from your shopping cart.", viewModel.Message); var cart = ShoppingCart.GetCart(dbContext, httpContext); Assert.DoesNotContain((await cart.GetCartItems()), c => c.CartItemId == cartItemId); }
public async Task RemoveFromCart_RemovesItemFromCart() { // Arrange var cartId = "CartId_A"; var cartItemId = 3; var numberOfItem = 5; var unitPrice = 10; var httpContext = new DefaultHttpContext(); // Session and cart initialization var sessionFeature = new SessionFeature() { Session = CreateTestSession(), }; httpContext.SetFeature<ISessionFeature>(sessionFeature); httpContext.Session.SetString("Session", cartId); // DbContext initialization var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>(); var cartItems = CreateTestCartItems(cartId, unitPrice, numberOfItem); dbContext.AddRange(cartItems.Select(n => n.Album).Distinct()); dbContext.AddRange(cartItems); dbContext.SaveChanges(); // ServiceProvder initialization var serviceProviderFeature = new ServiceProvidersFeature(); httpContext.SetFeature<IServiceProvidersFeature>(serviceProviderFeature); // AntiForgery initialization serviceProviderFeature.RequestServices = _serviceProvider; var antiForgery = serviceProviderFeature.RequestServices.GetRequiredService<AntiForgery>(); var tokens = antiForgery.GetTokens(httpContext, "testToken"); // Header initialization for AntiForgery var headers = new KeyValuePair<string, string[]>( "RequestVerificationToken", new string[] { tokens.CookieToken + ":" + tokens.FormToken }); httpContext.Request.Headers.Add(headers); // Cotroller initialization var controller = new ShoppingCartController() { DbContext = dbContext, AntiForgery = antiForgery, }; controller.ActionContext.HttpContext = httpContext; // Act var result = await controller.RemoveFromCart(cartItemId, CancellationToken.None); // Assert var jsonResult = Assert.IsType<JsonResult>(result); var viewModel = Assert.IsType<ShoppingCartRemoveViewModel>(jsonResult.Value); Assert.Equal(numberOfItem - 1, viewModel.CartCount); Assert.Equal((numberOfItem - 1) * 10, viewModel.CartTotal); Assert.Equal(" has been removed from your shopping cart.", viewModel.Message); var cart = ShoppingCart.GetCart(dbContext, httpContext); Assert.False((await cart.GetCartItems()).Any(c => c.CartItemId == cartItemId)); }