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.

131 lines
4.9 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 cartController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
  46. cartController.ControllerContext.HttpContext = _contextMock.Object;
  47. var actionResult = await cartController.Index(fakeQuantities, action);
  48. //Assert
  49. var viewResult = Assert.IsType<ViewResult>(actionResult);
  50. }
  51. [Fact]
  52. public async Task Post_cart_checkout_success()
  53. {
  54. //Arrange
  55. var fakeBuyerId = "1";
  56. var action = "[ Checkout ]";
  57. var fakeBasket = GetFakeBasket(fakeBuyerId);
  58. var fakeQuantities = new Dictionary<string, int>()
  59. {
  60. ["fakeProdA"] = 1,
  61. ["fakeProdB"] = 2
  62. };
  63. _basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
  64. .Returns(Task.FromResult(fakeBasket));
  65. _basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny<BasketModel>()))
  66. .Returns(Task.FromResult(fakeBasket));
  67. //Act
  68. var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
  69. orderController.ControllerContext.HttpContext = _contextMock.Object;
  70. var actionResult = await orderController.Index(fakeQuantities, action);
  71. //Assert
  72. var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
  73. Assert.Equal("Order", redirectToActionResult.ControllerName);
  74. Assert.Equal("Create", redirectToActionResult.ActionName);
  75. }
  76. [Fact]
  77. public async Task Add_to_cart_success()
  78. {
  79. //Arrange
  80. var fakeCatalogItem = GetFakeCatalogItem();
  81. _basketServiceMock.Setup(x => x.AddItemToBasket(It.IsAny<ApplicationUser>(), It.IsAny<Int32>()))
  82. .Returns(Task.FromResult(1));
  83. //Act
  84. var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
  85. orderController.ControllerContext.HttpContext = _contextMock.Object;
  86. var actionResult = await orderController.AddToCart(fakeCatalogItem);
  87. //Assert
  88. var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
  89. Assert.Equal("Catalog", redirectToActionResult.ControllerName);
  90. Assert.Equal("Index", redirectToActionResult.ActionName);
  91. }
  92. private BasketModel GetFakeBasket(string buyerId)
  93. {
  94. return new BasketModel()
  95. {
  96. BuyerId = buyerId
  97. };
  98. }
  99. private CatalogItem GetFakeCatalogItem()
  100. {
  101. return new CatalogItem()
  102. {
  103. Id = 1,
  104. Name = "fakeName",
  105. CatalogBrand = "fakeBrand",
  106. CatalogType = "fakeType",
  107. CatalogBrandId = 2,
  108. CatalogTypeId = 5,
  109. Price = 20
  110. };
  111. }
  112. }
  113. }