public async Task AddressAndPayment_ReturnsOrderIfRequestCanceled() { // Arrange var context = new DefaultHttpContext(); context.Request.Form = new FormCollection(new Dictionary <string, StringValues>()); var dbContext = _serviceProvider.GetRequiredService <MusicStoreContext>(); var controller = new CheckoutController(); controller.ControllerContext.HttpContext = context; var order = new Order(); // Act var result = await controller.AddressAndPayment(dbContext, order, new CancellationToken(true)); // Assert var viewResult = Assert.IsType <ViewResult>(result); Assert.Null(viewResult.ViewName); Assert.NotNull(viewResult.ViewData); Assert.Same(order, viewResult.ViewData.Model); }
public async Task AddressAndPayment_RedirectToCompleteWhenSuccessful() { // Arrange var httpContext = new DefaultHttpContext(); var orderId = 10; var order = new Order() { OrderId = orderId, }; // Session initialization var cartId = "CartId_A"; httpContext.Session = new TestSession(); httpContext.Session.SetString("Session", cartId); // FormCollection initialization httpContext.Request.Form = new FormCollection( new Dictionary <string, StringValues>() { { "PromoCode", new[] { "FREE" } } } ); // UserName initialization var claims = new List <Claim> { new Claim(ClaimTypes.Name, "TestUserName") }; httpContext.User = new ClaimsPrincipal(new ClaimsIdentity(claims)); // DbContext initialization var dbContext = _serviceProvider.GetRequiredService <MusicStoreContext>(); var cartItems = CreateTestCartItems( cartId, itemPrice: 10, numberOfItem: 1); dbContext.AddRange(cartItems.Select(n => n.Album).Distinct()); dbContext.AddRange(cartItems); dbContext.SaveChanges(); var controller = new CheckoutController(); controller.ControllerContext.HttpContext = httpContext; // Act var result = await controller.AddressAndPayment(dbContext, order, CancellationToken.None); // Assert var redirectResult = Assert.IsType <RedirectToActionResult>(result); Assert.Equal("Complete", redirectResult.ActionName); Assert.Null(redirectResult.ControllerName); Assert.NotNull(redirectResult.RouteValues); Assert.Equal(orderId, redirectResult.RouteValues["Id"]); }
public async Task AddressAndPayment_ReturnsOrderIfInvalidPromoCode() { // Arrange var context = new DefaultHttpContext(); var dbContext = _serviceProvider.GetRequiredService <MusicStoreContext>(); // AddressAndPayment action reads the Promo code from FormCollection. context.Request.Form = new FormCollection(new Dictionary <string, StringValues>()); var controller = new CheckoutController(); controller.ControllerContext.HttpContext = context; // Do not need actual data for Order; the Order object will be checked for the reference equality. var order = new Order(); // Act var result = await controller.AddressAndPayment(dbContext, order, CancellationToken.None); // Assert var viewResult = Assert.IsType <ViewResult>(result); Assert.Null(viewResult.ViewName); Assert.NotNull(viewResult.ViewData); Assert.Same(order, viewResult.ViewData.Model); }
public void AddressAndPayment_ReturnsDefaultView() { // Arrange var controller = new CheckoutController(); // Act var result = controller.AddressAndPayment(); // Assert var viewResult = Assert.IsType<ViewResult>(result); Assert.Null(viewResult.ViewName); }
public void AddressAndPayment_ReturnsDefaultView() { // Arrange var controller = new CheckoutController(); // Act var result = controller.AddressAndPayment(); // Assert var viewResult = Assert.IsType <ViewResult>(result); Assert.Null(viewResult.ViewName); }
public void AddressAndPayment_ReturnsDefaultView() { // Arrange var controller = new CheckoutController(_fixture.ServiceProvider.GetService <ILogger <CheckoutController> >()); // Act var result = controller.AddressAndPayment(); // Assert var viewResult = Assert.IsType <ViewResult>(result); Assert.Null(viewResult.ViewName); }
public async Task AddressAndPayment_ReturnsOrderIfInvalidOrderModel() { // Arrange var controller = new CheckoutController(); controller.ModelState.AddModelError("a", "ModelErrorA"); var order = new Order(); // Act var result = await controller.AddressAndPayment(order, CancellationToken.None); // Assert var viewResult = Assert.IsType <ViewResult>(result); Assert.Null(viewResult.ViewName); Assert.NotNull(viewResult.ViewData); Assert.Same(order, viewResult.ViewData.Model); }
public async Task AddressAndPayment_ReturnsOrderIfInvalidOrderModel() { // Arrange var controller = new CheckoutController(_serviceProvider.GetService <ILogger <CheckoutController> >()); controller.ModelState.AddModelError("a", "ModelErrorA"); var dbContext = _serviceProvider.GetRequiredService <MusicStoreContext>(); var order = new Order(); // Act var result = await controller.AddressAndPayment(dbContext, order, CancellationToken.None); // Assert var viewResult = Assert.IsType <ViewResult>(result); Assert.Null(viewResult.ViewName); Assert.NotNull(viewResult.ViewData); Assert.Same(order, viewResult.ViewData.Model); }
public async Task AddressAndPayment_ReturnsOrderIfInvalidPromoCode() { // Arrange var context = new DefaultHttpContext(); var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>(); // AddressAndPayment action reads the Promo code from FormCollection. context.Request.Form = new FormCollection(new Dictionary<string, StringValues>()); var controller = new CheckoutController(); controller.ControllerContext.HttpContext = context; // Do not need actual data for Order; the Order object will be checked for the reference equality. var order = new Order(); // Act var result = await controller.AddressAndPayment(dbContext, order, CancellationToken.None); // Assert var viewResult = Assert.IsType<ViewResult>(result); Assert.Null(viewResult.ViewName); Assert.NotNull(viewResult.ViewData); Assert.Same(order, viewResult.ViewData.Model); }
public async Task AddressAndPayment_RedirectToCompleteWhenSuccessful() { // Arrange var httpContext = new DefaultHttpContext(); var orderId = 10; var order = new Order() { OrderId = orderId, }; // Session initialization var cartId = "CartId_A"; httpContext.Session = new TestSession(); httpContext.Session.SetString("Session", cartId); // FormCollection initialization httpContext.Request.Form = new FormCollection( new Dictionary<string, StringValues>() { { "PromoCode", new[] { "FREE" } } } ); // UserName initialization var claims = new List<Claim> { new Claim(ClaimTypes.Name, "TestUserName") }; httpContext.User = new ClaimsPrincipal(new ClaimsIdentity(claims)); // DbContext initialization var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>(); var cartItems = CreateTestCartItems( cartId, itemPrice: 10, numberOfItem: 1); dbContext.AddRange(cartItems.Select(n => n.Album).Distinct()); dbContext.AddRange(cartItems); dbContext.SaveChanges(); var controller = new CheckoutController(); controller.ControllerContext.HttpContext = httpContext; // Act var result = await controller.AddressAndPayment(dbContext, order, CancellationToken.None); // Assert var redirectResult = Assert.IsType<RedirectToActionResult>(result); Assert.Equal("Complete", redirectResult.ActionName); Assert.Null(redirectResult.ControllerName); Assert.NotNull(redirectResult.RouteValues); Assert.Equal(orderId, redirectResult.RouteValues["Id"]); }
public async Task AddressAndPayment_ReturnsOrderIfInvalidOrderModel() { // Arrange var controller = new CheckoutController(); controller.ModelState.AddModelError("a", "ModelErrorA"); var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>(); var order = new Order(); // Act var result = await controller.AddressAndPayment(dbContext, order, CancellationToken.None); // Assert var viewResult = Assert.IsType<ViewResult>(result); Assert.Null(viewResult.ViewName); Assert.NotNull(viewResult.ViewData); Assert.Same(order, viewResult.ViewData.Model); }
public async Task AddressAndPayment_ReturnsOrderIfRequestCanceled() { // Arrange var context = new DefaultHttpContext(); context.Request.Form = new FormCollection(new Dictionary<string, StringValues>()); var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>(); var controller = new CheckoutController(); controller.ControllerContext.HttpContext = context; var order = new Order(); // Act var result = await controller.AddressAndPayment(dbContext, order, new CancellationToken(true)); // Assert var viewResult = Assert.IsType<ViewResult>(result); Assert.Null(viewResult.ViewName); Assert.NotNull(viewResult.ViewData); Assert.Same(order, viewResult.ViewData.Model); }