You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
5.0 KiB

  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.eShopOnContainers.WebMVC.Controllers;
  4. using Microsoft.eShopOnContainers.WebMVC.Services;
  5. using Microsoft.eShopOnContainers.WebMVC.ViewModels;
  6. using Moq;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Xunit;
  12. using BasketModel = Microsoft.eShopOnContainers.WebMVC.ViewModels.Basket;
  13. namespace UnitTest.Basket.Application
  14. {
  15. public class CartControllerTest
  16. {
  17. private readonly Mock<ICatalogService> _catalogServiceMock;
  18. private readonly Mock<IBasketService> _basketServiceMock;
  19. private readonly Mock<IIdentityParser<ApplicationUser>> _identityParserMock;
  20. private readonly Mock<HttpContext> _contextMock;
  21. public CartControllerTest()
  22. {
  23. _catalogServiceMock = new Mock<ICatalogService>();
  24. _basketServiceMock = new Mock<IBasketService>();
  25. _identityParserMock = new Mock<IIdentityParser<ApplicationUser>>();
  26. _contextMock = new Mock<HttpContext>();
  27. }
  28. [Fact]
  29. public async Task Post_cart_success()
  30. {
  31. //Arrange
  32. var fakeBuyerId = "1";
  33. var action = string.Empty;
  34. var fakeBasket = GetFakeBasket(fakeBuyerId);
  35. var fakeQuantities = new Dictionary<string, int>()
  36. {
  37. ["fakeProdA"] = 1,
  38. ["fakeProdB"] = 2
  39. };
  40. _basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
  41. .Returns(Task.FromResult(fakeBasket));
  42. _basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny<BasketModel>()))
  43. .Returns(Task.FromResult(fakeBasket));
  44. //Act
  45. var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
  46. orderController.ControllerContext.HttpContext = _contextMock.Object;
  47. var actionResult = await orderController.Index(fakeQuantities, action);
  48. //Assert
  49. var viewResult = Assert.IsType<ViewResult>(actionResult);
  50. var model = Assert.IsAssignableFrom<BasketModel>(viewResult.ViewData.Model);
  51. }
  52. [Fact]
  53. public async Task Post_cart_checkout_success()
  54. {
  55. //Arrange
  56. var fakeBuyerId = "1";
  57. var action = "[ Checkout ]";
  58. var fakeBasket = GetFakeBasket(fakeBuyerId);
  59. var fakeQuantities = new Dictionary<string, int>()
  60. {
  61. ["fakeProdA"] = 1,
  62. ["fakeProdB"] = 2
  63. };
  64. _basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
  65. .Returns(Task.FromResult(fakeBasket));
  66. _basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny<BasketModel>()))
  67. .Returns(Task.FromResult(fakeBasket));
  68. //Act
  69. var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
  70. orderController.ControllerContext.HttpContext = _contextMock.Object;
  71. var actionResult = await orderController.Index(fakeQuantities, action);
  72. //Assert
  73. var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
  74. Assert.Equal("Order", redirectToActionResult.ControllerName);
  75. Assert.Equal("Create", redirectToActionResult.ActionName);
  76. }
  77. [Fact]
  78. public async Task Add_to_cart_success()
  79. {
  80. //Arrange
  81. var fakeCatalogItem = GetFakeCatalogItem();
  82. _basketServiceMock.Setup(x => x.AddItemToBasket(It.IsAny<ApplicationUser>(), It.IsAny<BasketItem>()))
  83. .Returns(Task.FromResult(1));
  84. //Act
  85. var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
  86. orderController.ControllerContext.HttpContext = _contextMock.Object;
  87. var actionResult = await orderController.AddToCart(fakeCatalogItem);
  88. //Assert
  89. var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
  90. Assert.Equal("Catalog", redirectToActionResult.ControllerName);
  91. Assert.Equal("Index", redirectToActionResult.ActionName);
  92. }
  93. private BasketModel GetFakeBasket(string buyerId)
  94. {
  95. return new BasketModel()
  96. {
  97. BuyerId = buyerId
  98. };
  99. }
  100. private CatalogItem GetFakeCatalogItem()
  101. {
  102. return new CatalogItem()
  103. {
  104. Id = "1",
  105. Name = "fakeName",
  106. CatalogBrand = "fakeBrand",
  107. CatalogType = "fakeType",
  108. CatalogBrandId = 2,
  109. CatalogTypeId = 5,
  110. Price = 20
  111. };
  112. }
  113. }
  114. }