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.

69 lines
2.4 KiB

  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.eShopOnContainers.Services.Basket.API.Controllers;
  3. using Microsoft.eShopOnContainers.Services.Basket.API.Model;
  4. using Moq;
  5. using System.Collections.Generic;
  6. using System.Threading.Tasks;
  7. using Xunit;
  8. namespace UnitTest.Basket.Application
  9. {
  10. public class BasketWebApiTest
  11. {
  12. private readonly Mock<IBasketRepository> _basketRepositoryMock;
  13. public BasketWebApiTest()
  14. {
  15. _basketRepositoryMock = new Mock<IBasketRepository>();
  16. }
  17. [Fact]
  18. public async Task Get_customer_basket_success()
  19. {
  20. //Arrange
  21. var fakeCustomerId = "1";
  22. var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
  23. _basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
  24. .Returns(Task.FromResult(fakeCustomerBasket));
  25. //Act
  26. var basketController = new BasketController(_basketRepositoryMock.Object);
  27. var actionResult = await basketController.Get(fakeCustomerId) as OkObjectResult;
  28. //Assert
  29. Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
  30. Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
  31. }
  32. [Fact]
  33. public async Task Post_customer_basket_success()
  34. {
  35. //Arrange
  36. var fakeCustomerId = "1";
  37. var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
  38. _basketRepositoryMock.Setup(x => x.UpdateBasketAsync(It.IsAny<CustomerBasket>()))
  39. .Returns(Task.FromResult(fakeCustomerBasket));
  40. //Act
  41. var basketController = new BasketController(_basketRepositoryMock.Object);
  42. var actionResult = await basketController.Post(fakeCustomerBasket) as OkObjectResult;
  43. //Assert
  44. Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
  45. Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
  46. }
  47. private CustomerBasket GetCustomerBasketFake(string fakeCustomerId)
  48. {
  49. return new CustomerBasket(fakeCustomerId)
  50. {
  51. Items = new List<BasketItem>()
  52. {
  53. new BasketItem()
  54. }
  55. };
  56. }
  57. }
  58. }