Moved namespaces to Basket.UnitTest project
This commit is contained in:
parent
6064e2710f
commit
136c1dcbc4
@ -1,155 +1,140 @@
|
|||||||
using Basket.API.IntegrationEvents.Events;
|
namespace UnitTest.Basket.Application;
|
||||||
using Basket.API.Model;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
|
||||||
using Microsoft.eShopOnContainers.Services.Basket.API.Controllers;
|
|
||||||
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
|
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Moq;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Security.Claims;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Xunit;
|
|
||||||
using IBasketIdentityService = Microsoft.eShopOnContainers.Services.Basket.API.Services.IIdentityService;
|
|
||||||
|
|
||||||
namespace UnitTest.Basket.Application
|
public class BasketWebApiTest
|
||||||
{
|
{
|
||||||
public class BasketWebApiTest
|
private readonly Mock<IBasketRepository> _basketRepositoryMock;
|
||||||
|
private readonly Mock<IBasketIdentityService> _identityServiceMock;
|
||||||
|
private readonly Mock<IEventBus> _serviceBusMock;
|
||||||
|
private readonly Mock<ILogger<BasketController>> _loggerMock;
|
||||||
|
|
||||||
|
public BasketWebApiTest()
|
||||||
{
|
{
|
||||||
private readonly Mock<IBasketRepository> _basketRepositoryMock;
|
_basketRepositoryMock = new Mock<IBasketRepository>();
|
||||||
private readonly Mock<IBasketIdentityService> _identityServiceMock;
|
_identityServiceMock = new Mock<IBasketIdentityService>();
|
||||||
private readonly Mock<IEventBus> _serviceBusMock;
|
_serviceBusMock = new Mock<IEventBus>();
|
||||||
private readonly Mock<ILogger<BasketController>> _loggerMock;
|
_loggerMock = new Mock<ILogger<BasketController>>();
|
||||||
|
}
|
||||||
|
|
||||||
public BasketWebApiTest()
|
[Fact]
|
||||||
{
|
public async Task Get_customer_basket_success()
|
||||||
_basketRepositoryMock = new Mock<IBasketRepository>();
|
{
|
||||||
_identityServiceMock = new Mock<IBasketIdentityService>();
|
//Arrange
|
||||||
_serviceBusMock = new Mock<IEventBus>();
|
var fakeCustomerId = "1";
|
||||||
_loggerMock = new Mock<ILogger<BasketController>>();
|
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
||||||
public async Task Get_customer_basket_success()
|
.Returns(Task.FromResult(fakeCustomerBasket));
|
||||||
{
|
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||||
//Arrange
|
|
||||||
var fakeCustomerId = "1";
|
|
||||||
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
|
||||||
|
|
||||||
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
_serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var basketController = new BasketController(
|
||||||
|
_loggerMock.Object,
|
||||||
|
_basketRepositoryMock.Object,
|
||||||
|
_identityServiceMock.Object,
|
||||||
|
_serviceBusMock.Object);
|
||||||
|
|
||||||
|
var actionResult = await basketController.GetBasketByIdAsync(fakeCustomerId);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK);
|
||||||
|
Assert.Equal((((ObjectResult)actionResult.Result).Value as CustomerBasket).BuyerId, fakeCustomerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Post_customer_basket_success()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fakeCustomerId = "1";
|
||||||
|
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
||||||
|
|
||||||
|
_basketRepositoryMock.Setup(x => x.UpdateBasketAsync(It.IsAny<CustomerBasket>()))
|
||||||
|
.Returns(Task.FromResult(fakeCustomerBasket));
|
||||||
|
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||||
|
_serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var basketController = new BasketController(
|
||||||
|
_loggerMock.Object,
|
||||||
|
_basketRepositoryMock.Object,
|
||||||
|
_identityServiceMock.Object,
|
||||||
|
_serviceBusMock.Object);
|
||||||
|
|
||||||
|
var actionResult = await basketController.UpdateBasketAsync(fakeCustomerBasket);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK);
|
||||||
|
Assert.Equal((((ObjectResult)actionResult.Result).Value as CustomerBasket).BuyerId, fakeCustomerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Doing_Checkout_Without_Basket_Should_Return_Bad_Request()
|
||||||
|
{
|
||||||
|
var fakeCustomerId = "2";
|
||||||
|
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
||||||
|
.Returns(Task.FromResult((CustomerBasket)null));
|
||||||
|
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var basketController = new BasketController(
|
||||||
|
_loggerMock.Object,
|
||||||
|
_basketRepositoryMock.Object,
|
||||||
|
_identityServiceMock.Object,
|
||||||
|
_serviceBusMock.Object);
|
||||||
|
|
||||||
|
var result = await basketController.CheckoutAsync(new BasketCheckout(), Guid.NewGuid().ToString()) as BadRequestResult;
|
||||||
|
Assert.NotNull(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Doing_Checkout_Wit_Basket_Should_Publish_UserCheckoutAccepted_Integration_Event()
|
||||||
|
{
|
||||||
|
var fakeCustomerId = "1";
|
||||||
|
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
||||||
|
|
||||||
|
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
||||||
.Returns(Task.FromResult(fakeCustomerBasket));
|
.Returns(Task.FromResult(fakeCustomerBasket));
|
||||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
|
||||||
|
|
||||||
_serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
|
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||||
|
|
||||||
//Act
|
var basketController = new BasketController(
|
||||||
var basketController = new BasketController(
|
_loggerMock.Object,
|
||||||
_loggerMock.Object,
|
_basketRepositoryMock.Object,
|
||||||
_basketRepositoryMock.Object,
|
_identityServiceMock.Object,
|
||||||
_identityServiceMock.Object,
|
_serviceBusMock.Object);
|
||||||
_serviceBusMock.Object);
|
|
||||||
|
|
||||||
var actionResult = await basketController.GetBasketByIdAsync(fakeCustomerId);
|
basketController.ControllerContext = new ControllerContext()
|
||||||
|
|
||||||
//Assert
|
|
||||||
Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK);
|
|
||||||
Assert.Equal((((ObjectResult)actionResult.Result).Value as CustomerBasket).BuyerId, fakeCustomerId);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task Post_customer_basket_success()
|
|
||||||
{
|
{
|
||||||
//Arrange
|
HttpContext = new DefaultHttpContext()
|
||||||
var fakeCustomerId = "1";
|
|
||||||
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
|
||||||
|
|
||||||
_basketRepositoryMock.Setup(x => x.UpdateBasketAsync(It.IsAny<CustomerBasket>()))
|
|
||||||
.Returns(Task.FromResult(fakeCustomerBasket));
|
|
||||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
|
||||||
_serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
|
|
||||||
|
|
||||||
//Act
|
|
||||||
var basketController = new BasketController(
|
|
||||||
_loggerMock.Object,
|
|
||||||
_basketRepositoryMock.Object,
|
|
||||||
_identityServiceMock.Object,
|
|
||||||
_serviceBusMock.Object);
|
|
||||||
|
|
||||||
var actionResult = await basketController.UpdateBasketAsync(fakeCustomerBasket);
|
|
||||||
|
|
||||||
//Assert
|
|
||||||
Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK);
|
|
||||||
Assert.Equal((((ObjectResult)actionResult.Result).Value as CustomerBasket).BuyerId, fakeCustomerId);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task Doing_Checkout_Without_Basket_Should_Return_Bad_Request()
|
|
||||||
{
|
|
||||||
var fakeCustomerId = "2";
|
|
||||||
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
|
||||||
.Returns(Task.FromResult((CustomerBasket)null));
|
|
||||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
|
||||||
|
|
||||||
//Act
|
|
||||||
var basketController = new BasketController(
|
|
||||||
_loggerMock.Object,
|
|
||||||
_basketRepositoryMock.Object,
|
|
||||||
_identityServiceMock.Object,
|
|
||||||
_serviceBusMock.Object);
|
|
||||||
|
|
||||||
var result = await basketController.CheckoutAsync(new BasketCheckout(), Guid.NewGuid().ToString()) as BadRequestResult;
|
|
||||||
Assert.NotNull(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task Doing_Checkout_Wit_Basket_Should_Publish_UserCheckoutAccepted_Integration_Event()
|
|
||||||
{
|
|
||||||
var fakeCustomerId = "1";
|
|
||||||
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
|
||||||
|
|
||||||
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
|
|
||||||
.Returns(Task.FromResult(fakeCustomerBasket));
|
|
||||||
|
|
||||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
|
||||||
|
|
||||||
var basketController = new BasketController(
|
|
||||||
_loggerMock.Object,
|
|
||||||
_basketRepositoryMock.Object,
|
|
||||||
_identityServiceMock.Object,
|
|
||||||
_serviceBusMock.Object);
|
|
||||||
|
|
||||||
basketController.ControllerContext = new ControllerContext()
|
|
||||||
{
|
{
|
||||||
HttpContext = new DefaultHttpContext()
|
User = new ClaimsPrincipal(
|
||||||
{
|
new ClaimsIdentity(new Claim[] {
|
||||||
User = new ClaimsPrincipal(
|
new Claim("sub", "testuser"),
|
||||||
new ClaimsIdentity(new Claim[] {
|
new Claim("unique_name", "testuser"),
|
||||||
new Claim("sub", "testuser"),
|
new Claim(ClaimTypes.Name, "testuser")
|
||||||
new Claim("unique_name", "testuser"),
|
}))
|
||||||
new Claim(ClaimTypes.Name, "testuser")
|
}
|
||||||
}))
|
};
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
//Act
|
//Act
|
||||||
var result = await basketController.CheckoutAsync(new BasketCheckout(), Guid.NewGuid().ToString()) as AcceptedResult;
|
var result = await basketController.CheckoutAsync(new BasketCheckout(), Guid.NewGuid().ToString()) as AcceptedResult;
|
||||||
|
|
||||||
_serviceBusMock.Verify(mock => mock.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()), Times.Once);
|
_serviceBusMock.Verify(mock => mock.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()), Times.Once);
|
||||||
|
|
||||||
Assert.NotNull(result);
|
Assert.NotNull(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
private CustomerBasket GetCustomerBasketFake(string fakeCustomerId)
|
private CustomerBasket GetCustomerBasketFake(string fakeCustomerId)
|
||||||
|
{
|
||||||
|
return new CustomerBasket(fakeCustomerId)
|
||||||
{
|
{
|
||||||
return new CustomerBasket(fakeCustomerId)
|
Items = new List<BasketItem>()
|
||||||
{
|
{
|
||||||
Items = new List<BasketItem>()
|
new BasketItem()
|
||||||
{
|
}
|
||||||
new BasketItem()
|
};
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,130 +1,117 @@
|
|||||||
using Microsoft.AspNetCore.Http;
|
namespace UnitTest.Basket.Application;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.eShopOnContainers.WebMVC.Controllers;
|
|
||||||
using Microsoft.eShopOnContainers.WebMVC.Services;
|
|
||||||
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
|
|
||||||
using Moq;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Xunit;
|
|
||||||
using BasketModel = Microsoft.eShopOnContainers.WebMVC.ViewModels.Basket;
|
|
||||||
|
|
||||||
namespace UnitTest.Basket.Application
|
public class CartControllerTest
|
||||||
{
|
{
|
||||||
public class CartControllerTest
|
private readonly Mock<ICatalogService> _catalogServiceMock;
|
||||||
|
private readonly Mock<IBasketService> _basketServiceMock;
|
||||||
|
private readonly Mock<IIdentityParser<ApplicationUser>> _identityParserMock;
|
||||||
|
private readonly Mock<HttpContext> _contextMock;
|
||||||
|
|
||||||
|
public CartControllerTest()
|
||||||
{
|
{
|
||||||
private readonly Mock<ICatalogService> _catalogServiceMock;
|
_catalogServiceMock = new Mock<ICatalogService>();
|
||||||
private readonly Mock<IBasketService> _basketServiceMock;
|
_basketServiceMock = new Mock<IBasketService>();
|
||||||
private readonly Mock<IIdentityParser<ApplicationUser>> _identityParserMock;
|
_identityParserMock = new Mock<IIdentityParser<ApplicationUser>>();
|
||||||
private readonly Mock<HttpContext> _contextMock;
|
_contextMock = new Mock<HttpContext>();
|
||||||
|
}
|
||||||
|
|
||||||
public CartControllerTest()
|
[Fact]
|
||||||
|
public async Task Post_cart_success()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fakeBuyerId = "1";
|
||||||
|
var action = string.Empty;
|
||||||
|
var fakeBasket = GetFakeBasket(fakeBuyerId);
|
||||||
|
var fakeQuantities = new Dictionary<string, int>()
|
||||||
{
|
{
|
||||||
_catalogServiceMock = new Mock<ICatalogService>();
|
["fakeProdA"] = 1,
|
||||||
_basketServiceMock = new Mock<IBasketService>();
|
["fakeProdB"] = 2
|
||||||
_identityParserMock = new Mock<IIdentityParser<ApplicationUser>>();
|
};
|
||||||
_contextMock = new Mock<HttpContext>();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
_basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
|
||||||
public async Task Post_cart_success()
|
.Returns(Task.FromResult(fakeBasket));
|
||||||
|
|
||||||
|
_basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny<BasketModel>()))
|
||||||
|
.Returns(Task.FromResult(fakeBasket));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var cartController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
||||||
|
cartController.ControllerContext.HttpContext = _contextMock.Object;
|
||||||
|
var actionResult = await cartController.Index(fakeQuantities, action);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
var viewResult = Assert.IsType<ViewResult>(actionResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Post_cart_checkout_success()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fakeBuyerId = "1";
|
||||||
|
var action = "[ Checkout ]";
|
||||||
|
var fakeBasket = GetFakeBasket(fakeBuyerId);
|
||||||
|
var fakeQuantities = new Dictionary<string, int>()
|
||||||
{
|
{
|
||||||
//Arrange
|
["fakeProdA"] = 1,
|
||||||
var fakeBuyerId = "1";
|
["fakeProdB"] = 2
|
||||||
var action = string.Empty;
|
};
|
||||||
var fakeBasket = GetFakeBasket(fakeBuyerId);
|
|
||||||
var fakeQuantities = new Dictionary<string, int>()
|
|
||||||
{
|
|
||||||
["fakeProdA"] = 1,
|
|
||||||
["fakeProdB"] = 2
|
|
||||||
};
|
|
||||||
|
|
||||||
_basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
|
_basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
|
||||||
.Returns(Task.FromResult(fakeBasket));
|
.Returns(Task.FromResult(fakeBasket));
|
||||||
|
|
||||||
_basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny<BasketModel>()))
|
_basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny<BasketModel>()))
|
||||||
.Returns(Task.FromResult(fakeBasket));
|
.Returns(Task.FromResult(fakeBasket));
|
||||||
|
|
||||||
//Act
|
//Act
|
||||||
var cartController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
||||||
cartController.ControllerContext.HttpContext = _contextMock.Object;
|
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
||||||
var actionResult = await cartController.Index(fakeQuantities, action);
|
var actionResult = await orderController.Index(fakeQuantities, action);
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
var viewResult = Assert.IsType<ViewResult>(actionResult);
|
var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
|
||||||
}
|
Assert.Equal("Order", redirectToActionResult.ControllerName);
|
||||||
|
Assert.Equal("Create", redirectToActionResult.ActionName);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Post_cart_checkout_success()
|
public async Task Add_to_cart_success()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fakeCatalogItem = GetFakeCatalogItem();
|
||||||
|
|
||||||
|
_basketServiceMock.Setup(x => x.AddItemToBasket(It.IsAny<ApplicationUser>(), It.IsAny<Int32>()))
|
||||||
|
.Returns(Task.FromResult(1));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
||||||
|
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
||||||
|
var actionResult = await orderController.AddToCart(fakeCatalogItem);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
|
||||||
|
Assert.Equal("Catalog", redirectToActionResult.ControllerName);
|
||||||
|
Assert.Equal("Index", redirectToActionResult.ActionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private BasketModel GetFakeBasket(string buyerId)
|
||||||
|
{
|
||||||
|
return new BasketModel()
|
||||||
{
|
{
|
||||||
//Arrange
|
BuyerId = buyerId
|
||||||
var fakeBuyerId = "1";
|
};
|
||||||
var action = "[ Checkout ]";
|
}
|
||||||
var fakeBasket = GetFakeBasket(fakeBuyerId);
|
|
||||||
var fakeQuantities = new Dictionary<string, int>()
|
|
||||||
{
|
|
||||||
["fakeProdA"] = 1,
|
|
||||||
["fakeProdB"] = 2
|
|
||||||
};
|
|
||||||
|
|
||||||
_basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
|
private CatalogItem GetFakeCatalogItem()
|
||||||
.Returns(Task.FromResult(fakeBasket));
|
{
|
||||||
|
return new CatalogItem()
|
||||||
_basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny<BasketModel>()))
|
|
||||||
.Returns(Task.FromResult(fakeBasket));
|
|
||||||
|
|
||||||
//Act
|
|
||||||
var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
|
||||||
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
|
||||||
var actionResult = await orderController.Index(fakeQuantities, action);
|
|
||||||
|
|
||||||
//Assert
|
|
||||||
var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
|
|
||||||
Assert.Equal("Order", redirectToActionResult.ControllerName);
|
|
||||||
Assert.Equal("Create", redirectToActionResult.ActionName);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task Add_to_cart_success()
|
|
||||||
{
|
{
|
||||||
//Arrange
|
Id = 1,
|
||||||
var fakeCatalogItem = GetFakeCatalogItem();
|
Name = "fakeName",
|
||||||
|
CatalogBrand = "fakeBrand",
|
||||||
_basketServiceMock.Setup(x => x.AddItemToBasket(It.IsAny<ApplicationUser>(), It.IsAny<Int32>()))
|
CatalogType = "fakeType",
|
||||||
.Returns(Task.FromResult(1));
|
CatalogBrandId = 2,
|
||||||
|
CatalogTypeId = 5,
|
||||||
//Act
|
Price = 20
|
||||||
var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object);
|
};
|
||||||
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
|
||||||
var actionResult = await orderController.AddToCart(fakeCatalogItem);
|
|
||||||
|
|
||||||
//Assert
|
|
||||||
var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
|
|
||||||
Assert.Equal("Catalog", redirectToActionResult.ControllerName);
|
|
||||||
Assert.Equal("Index", redirectToActionResult.ActionName);
|
|
||||||
}
|
|
||||||
|
|
||||||
private BasketModel GetFakeBasket(string buyerId)
|
|
||||||
{
|
|
||||||
return new BasketModel()
|
|
||||||
{
|
|
||||||
BuyerId = buyerId
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private CatalogItem GetFakeCatalogItem()
|
|
||||||
{
|
|
||||||
return new CatalogItem()
|
|
||||||
{
|
|
||||||
Id = 1,
|
|
||||||
Name = "fakeName",
|
|
||||||
CatalogBrand = "fakeBrand",
|
|
||||||
CatalogType = "fakeType",
|
|
||||||
CatalogBrandId = 2,
|
|
||||||
CatalogTypeId = 5,
|
|
||||||
Price = 20
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user