public void AddItemWithValidId() { // Act var resultRaw = _controller.AddItem(1, 1, 2); NegotiatedContentResult <ResponseDto> result = (NegotiatedContentResult <ResponseDto>)resultRaw; ////// Assert Assert.IsNotNull(result); Assert.IsNotNull(result.Content); Assert.AreEqual(HttpStatusCode.OK, result.Content.StatusCode); }
public void AddCartItem(Guid SanPhamID, int SoLuong) { DoAnWeb.Controllers.CartController ctrl = new CartController(); ActionResult result = ctrl.AddItem(SanPhamID, SoLuong); Assert.AreNotEqual(null, result); }
/// <summary> /// Adds item to shopping cart and redirect to shopping cart page. /// </summary> protected void ButtonAddToCart_Click(object sender, EventArgs e) { Page.Validate(); if (!Page.IsValid) { return; } // Retrieve product via Product Facade. ProductController controller = new ProductController(); ActionServiceReference.Product product = controller.GetProduct(ProductId); // Get product details and add information to cart. int productId = product.ProductId; string name = product.ProductName; double unitPrice = product.UnitPrice; int quantity; if (!int.TryParse(TextBoxQuantity.Text.Trim(), out quantity)) { quantity = 1; } CartController cartController = new CartController(); cartController.AddItem(productId, name, quantity, unitPrice); // Show shopping cart to user. Response.Redirect("Cart.aspx"); }
public void TestAddProductOnCartWithoutToken() { var mockCartService = new Mock <ICartService>(); var mockUserService = new Mock <IUserService>(); mockCartService.Setup(x => x.AddProduct(cartRequest)); var controller = new CartController(mockCartService.Object, mockUserService.Object); IHttpActionResult response = controller.AddItem(cartRequest); var contentResult = response as OkNegotiatedContentResult <ControllerResponse>; Assert.IsNotNull(contentResult.Content.Message); }
public void TestAddProductWithoutLogin() { var mockCartService = new Mock <ICartService>(); var mockUserService = new Mock <IUserService>(); mockCartService.Setup(x => x.AddProduct(cartRequest)); var controller = new CartController(mockCartService.Object, mockUserService.Object); var controllerContext = new HttpControllerContext(); var request = new HttpRequestMessage(); request.Headers.Add(ControllerHelper.TOKEN_NAME, new Guid().ToString()); controllerContext.Request = request; controller.ControllerContext = controllerContext; IHttpActionResult response = controller.AddItem(cartRequest); var contentResult = response as OkNegotiatedContentResult <ControllerResponse>; Assert.IsNotNull(contentResult.Content.Message); }
public void TestAddProductWithLogin() { string token = LoginContext.GetInstance().GenerateNewToken("1"); UserContextDTO userContextDTO = GetUserContextDTO(token); LoginContext.GetInstance().SaveContext(userContextDTO); var mockCartService = new Mock <ICartService>(); var mockUserService = new Mock <IUserService>(); mockCartService.Setup(x => x.AddProduct(cartRequest)); var controller = new CartController(mockCartService.Object, mockUserService.Object); var controllerContext = new HttpControllerContext(); var request = new HttpRequestMessage(); request.Headers.Add(ControllerHelper.TOKEN_NAME, token); controllerContext.Request = request; controller.ControllerContext = controllerContext; IHttpActionResult response = controller.AddItem(cartRequest); var contentResult = response as OkNegotiatedContentResult <ControllerResponse>; Assert.IsTrue(contentResult.Content.Success); }
public async void AddItem_CatchesOnSave() { Game game = new Game() { Id = GameId, Name = "game" }; GameProduct gameProduct = new PhysicalGameProduct() { Id = Id, BoxArtImageURL = "boxart", NewWebPrice = 79.99m, UsedWebPrice = 44.99m, Platform = new Platform { PlatformName = "PS4", }, Game = game }; Cart cart = new Cart { MemberId = UserId, Items = new List<CartItem>(), }; Member member = new Member() { UserId = UserId, Cart = cart }; Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake(); Mock<DbSet<Cart>> cartDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Cart> { cart }.AsQueryable()); Mock<DbSet<Member>> memberDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Member> { member }.AsQueryable()); Mock<DbSet<GameProduct>> gameProductDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<GameProduct> { gameProduct }.AsQueryable()); cartDbSetStub.Setup(db => db.FindAsync(cart.MemberId)).ReturnsAsync(cart); gameProductDbSetStub.Setup(db => db.FindAsync(gameProduct.Id)).ReturnsAsync(gameProduct); memberDbSetStub.Setup(db => db.Find(member.UserId)).Returns(member); gameProductDbSetStub.SetupForInclude(); dbStub.Setup(db => db.Carts).Returns(cartDbSetStub.Object); dbStub.Setup(db => db.GameProducts).Returns(gameProductDbSetStub.Object); dbStub.Setup(db => db.Members).Returns(memberDbSetStub.Object); dbStub.Setup(db => db.SaveChangesAsync()).Throws<DbUpdateException>(); Mock<ControllerContext> contextStub = new Mock<ControllerContext>(); contextStub.Setup(c => c.HttpContext.User.Identity).Returns<IIdentity>(null); contextStub.Setup(c => c.HttpContext.User.Identity.IsAuthenticated).Returns(true); Mock<IGuidUserIdGetter> idGetterStub = new Mock<IGuidUserIdGetter>(); idGetterStub.Setup(id => id.GetUserId(It.IsAny<IIdentity>())).Returns(UserId); CartController controller = new CartController(dbStub.Object, idGetterStub.Object, shippingCostService: null) { ControllerContext = contextStub.Object }; var result = await controller.AddItem(gameProduct.Id, true) as RedirectToRouteResult; Assert.That(result != null); Assert.That(result.RouteValues["action"], Is.EqualTo("Details")); }
public void AddItem_NullCart() { Game game = new Game() { Id = GameId, Name = "game" }; GameProduct gameProduct = new PhysicalGameProduct() { Id = Id, BoxArtImageURL = "boxart", NewWebPrice = 79.99m, UsedWebPrice = 44.99m, Platform = new Platform { PlatformName = "PS4", }, Game = game }; Cart cart = new Cart { MemberId = new Guid("45B0752E-998B-477A-AAAD-3ED535BA3559"), Items = new List<CartItem>(), }; Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake(); Mock<DbSet<Cart>> cartDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Cart> { cart }.AsQueryable()); Mock<DbSet<GameProduct>> gameProductDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<GameProduct> { gameProduct }.AsQueryable()); cartDbSetStub.Setup(db => db.FindAsync(cart.MemberId)).ReturnsAsync(cart); gameProductDbSetStub.Setup(db => db.FindAsync(gameProduct.Id)).ReturnsAsync(gameProduct); gameProductDbSetStub.SetupForInclude(); dbStub.Setup(db => db.Carts).Returns(cartDbSetStub.Object); dbStub.Setup(db => db.GameProducts).Returns(gameProductDbSetStub.Object); Mock<ControllerContext> contextStub = new Mock<ControllerContext>(); contextStub.Setup(c => c.HttpContext.User.Identity).Returns<IIdentity>(null); contextStub.Setup(c => c.HttpContext.User.Identity.IsAuthenticated).Returns(true); Mock<IGuidUserIdGetter> idGetterStub = new Mock<IGuidUserIdGetter>(); idGetterStub.Setup(id => id.GetUserId(It.IsAny<IIdentity>())).Returns(UserId); CartController controller = new CartController(dbStub.Object, idGetterStub.Object, shippingCostService: null) { ControllerContext = contextStub.Object }; Assert.That(async () => await controller.AddItem(gameProduct.Id, true), Throws.InstanceOf<HttpException>().And.Matches<HttpException>(ex => ex.GetHttpCode() == 404)); }
public void AddItem_NullId() { CartController controller = new CartController(veilDataAccess: null, idGetter: null, shippingCostService: null); Assert.That(async () => await controller.AddItem(null, true), Throws.InstanceOf<HttpException>().And.Matches<HttpException>(ex => ex.GetHttpCode() == 404)); }