Added unitary test cases
This commit is contained in:
parent
7db5119a87
commit
4e53f205b9
54
test/Services/UnitTest/Account/AccountControllerTest.cs
Normal file
54
test/Services/UnitTest/Account/AccountControllerTest.cs
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
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.Security.Claims;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace UnitTest.Account
|
||||||
|
{
|
||||||
|
public class AccountControllerTest
|
||||||
|
{
|
||||||
|
private readonly Mock<IIdentityParser<ApplicationUser>> _identityParserMock;
|
||||||
|
private readonly Mock<HttpContext> _httpContextMock;
|
||||||
|
|
||||||
|
public AccountControllerTest()
|
||||||
|
{
|
||||||
|
_identityParserMock = new Mock<IIdentityParser<ApplicationUser>>();
|
||||||
|
_httpContextMock = new Mock<HttpContext>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Signin_with_token_success()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fakeCP = GenerateFakeClaimsIdentity();
|
||||||
|
|
||||||
|
_httpContextMock.Setup(x => x.User)
|
||||||
|
.Returns(new ClaimsPrincipal(fakeCP));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var accountController = new AccountController(_identityParserMock.Object);
|
||||||
|
accountController.ControllerContext.HttpContext = _httpContextMock.Object;
|
||||||
|
var actionResult = accountController.SignIn("");
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
var redirectResult = Assert.IsType<RedirectResult>(actionResult);
|
||||||
|
Assert.Equal(redirectResult.Url, "/");
|
||||||
|
Assert.Equal(accountController.ViewData["access_token"], "fakeToken");
|
||||||
|
}
|
||||||
|
|
||||||
|
private ClaimsIdentity GenerateFakeClaimsIdentity()
|
||||||
|
{
|
||||||
|
var ci = new ClaimsIdentity();
|
||||||
|
ci.AddClaim(new Claim("access_token", "fakeToken"));
|
||||||
|
return ci;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Basket.API.Controllers;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
|
||||||
|
using Moq;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace UnitTest.Basket.Application
|
||||||
|
{
|
||||||
|
public class BasketWebApiTest
|
||||||
|
{
|
||||||
|
private readonly Mock<IBasketRepository> _basketRepositoryMock;
|
||||||
|
|
||||||
|
public BasketWebApiTest()
|
||||||
|
{
|
||||||
|
_basketRepositoryMock = new Mock<IBasketRepository>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Get_customer_basket_success()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fakeCustomerId = "1";
|
||||||
|
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
||||||
|
|
||||||
|
_basketRepositoryMock.Setup(x => x.GetBasket(It.IsAny<string>()))
|
||||||
|
.Returns(Task.FromResult(fakeCustomerBasket));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var basketController = new BasketController(_basketRepositoryMock.Object);
|
||||||
|
var actionResult = await basketController.Get(fakeCustomerId) as OkObjectResult;
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
|
||||||
|
Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Post_customer_basket_success()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fakeCustomerId = "1";
|
||||||
|
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
|
||||||
|
|
||||||
|
_basketRepositoryMock.Setup(x => x.UpdateBasket(It.IsAny<CustomerBasket>()))
|
||||||
|
.Returns(Task.FromResult(fakeCustomerBasket));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var basketController = new BasketController(_basketRepositoryMock.Object);
|
||||||
|
var actionResult = await basketController.Post(fakeCustomerBasket) as OkObjectResult;
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
|
||||||
|
Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CustomerBasket GetCustomerBasketFake(string fakeCustomerId)
|
||||||
|
{
|
||||||
|
return new CustomerBasket(fakeCustomerId)
|
||||||
|
{
|
||||||
|
Items = new List<BasketItem>()
|
||||||
|
{
|
||||||
|
new BasketItem()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
132
test/Services/UnitTest/Basket/Application/CartControllerTest.cs
Normal file
132
test/Services/UnitTest/Basket/Application/CartControllerTest.cs
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
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.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
using BasketModel = Microsoft.eShopOnContainers.WebMVC.ViewModels.Basket;
|
||||||
|
|
||||||
|
namespace UnitTest.Basket.Application
|
||||||
|
{
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
_catalogServiceMock = new Mock<ICatalogService>();
|
||||||
|
_basketServiceMock = new Mock<IBasketService>();
|
||||||
|
_identityParserMock = new Mock<IIdentityParser<ApplicationUser>>();
|
||||||
|
_contextMock = new Mock<HttpContext>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[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>()
|
||||||
|
{
|
||||||
|
["fakeProdA"] = 1,
|
||||||
|
["fakeProdB"] = 2
|
||||||
|
};
|
||||||
|
|
||||||
|
_basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
|
||||||
|
.Returns(Task.FromResult(fakeBasket));
|
||||||
|
|
||||||
|
_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 viewResult = Assert.IsType<ViewResult>(actionResult);
|
||||||
|
var model = Assert.IsAssignableFrom<BasketModel>(viewResult.ViewData.Model);
|
||||||
|
}
|
||||||
|
|
||||||
|
[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>()
|
||||||
|
{
|
||||||
|
["fakeProdA"] = 1,
|
||||||
|
["fakeProdB"] = 2
|
||||||
|
};
|
||||||
|
|
||||||
|
_basketServiceMock.Setup(x => x.SetQuantities(It.IsAny<ApplicationUser>(), It.IsAny<Dictionary<string, int>>()))
|
||||||
|
.Returns(Task.FromResult(fakeBasket));
|
||||||
|
|
||||||
|
_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
|
||||||
|
var fakeCatalogItem = GetFakeCatalogItem();
|
||||||
|
|
||||||
|
_basketServiceMock.Setup(x => x.AddItemToBasket(It.IsAny<ApplicationUser>(), It.IsAny<BasketItem>()))
|
||||||
|
.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()
|
||||||
|
{
|
||||||
|
BuyerId = buyerId
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private CatalogItem GetFakeCatalogItem()
|
||||||
|
{
|
||||||
|
return new CatalogItem()
|
||||||
|
{
|
||||||
|
Id = "1",
|
||||||
|
Name = "fakeName",
|
||||||
|
CatalogBrand = "fakeBrand",
|
||||||
|
CatalogType = "fakeType",
|
||||||
|
CatalogBrandId = 2,
|
||||||
|
CatalogTypeId = 5,
|
||||||
|
Price = 20
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,90 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.eShopOnContainers.WebMVC.Controllers;
|
||||||
|
using Microsoft.eShopOnContainers.WebMVC.Services;
|
||||||
|
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
|
||||||
|
using Microsoft.eShopOnContainers.WebMVC.ViewModels.CatalogViewModels;
|
||||||
|
using Moq;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
using CatalogModel = Microsoft.eShopOnContainers.WebMVC.ViewModels.Catalog;
|
||||||
|
|
||||||
|
namespace UnitTest.Catalog.Application
|
||||||
|
{
|
||||||
|
public class CatalogControllerTest
|
||||||
|
{
|
||||||
|
private readonly Mock<ICatalogService> _catalogServiceMock;
|
||||||
|
|
||||||
|
public CatalogControllerTest()
|
||||||
|
{
|
||||||
|
_catalogServiceMock = new Mock<ICatalogService>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Get_catalog_items_success()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fakeBrandFilterApplied = 1;
|
||||||
|
var fakeTypesFilterApplied = 2;
|
||||||
|
var fakePage = 2;
|
||||||
|
var fakeCatalog = GetFakeCatalog();
|
||||||
|
|
||||||
|
var expectedNumberOfPages = 5;
|
||||||
|
var expectedTotalPages = 50;
|
||||||
|
var expectedCurrentPage = 2;
|
||||||
|
|
||||||
|
_catalogServiceMock.Setup(x => x.GetCatalogItems
|
||||||
|
(
|
||||||
|
It.Is<int>(y => y == fakePage),
|
||||||
|
It.IsAny<int>(),
|
||||||
|
It.Is<int?>(y => y == fakeBrandFilterApplied),
|
||||||
|
It.Is<int?>(y => y == fakeTypesFilterApplied)
|
||||||
|
))
|
||||||
|
.Returns(Task.FromResult(fakeCatalog));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var orderController = new CatalogController(_catalogServiceMock.Object);
|
||||||
|
var actionResult = await orderController.Index(fakeBrandFilterApplied, fakeTypesFilterApplied, fakePage);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
var viewResult = Assert.IsType<ViewResult>(actionResult);
|
||||||
|
var model = Assert.IsAssignableFrom<IndexViewModel>(viewResult.ViewData.Model);
|
||||||
|
Assert.Equal(model.PaginationInfo.TotalPages, expectedNumberOfPages);
|
||||||
|
Assert.Equal(model.PaginationInfo.TotalItems, expectedTotalPages);
|
||||||
|
Assert.Equal(model.PaginationInfo.ActualPage, expectedCurrentPage);
|
||||||
|
Assert.Empty(model.PaginationInfo.Next);
|
||||||
|
Assert.Empty(model.PaginationInfo.Previous);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CatalogModel GetFakeCatalog()
|
||||||
|
{
|
||||||
|
return new CatalogModel()
|
||||||
|
{
|
||||||
|
PageSize = 10,
|
||||||
|
Count = 50,
|
||||||
|
PageIndex = 2,
|
||||||
|
Data = new List<CatalogItem>()
|
||||||
|
{
|
||||||
|
new CatalogItem()
|
||||||
|
{
|
||||||
|
Id = "1",
|
||||||
|
Name = "fakeItemA",
|
||||||
|
CatalogTypeId = 1
|
||||||
|
},
|
||||||
|
new CatalogItem()
|
||||||
|
{
|
||||||
|
Id = "2",
|
||||||
|
Name = "fakeItemB",
|
||||||
|
CatalogTypeId = 1
|
||||||
|
},
|
||||||
|
new CatalogItem()
|
||||||
|
{
|
||||||
|
Id = "3",
|
||||||
|
Name = "fakeItemC",
|
||||||
|
CatalogTypeId = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace UnitTest.Ordering.Application
|
||||||
|
{
|
||||||
|
using MediatR;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositories;
|
||||||
|
using Moq;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
public class IdentifierCommandHandlerTest
|
||||||
|
{
|
||||||
|
private readonly Mock<IRequestManager> _requestManager;
|
||||||
|
private readonly Mock<IMediator> _mediator;
|
||||||
|
|
||||||
|
public IdentifierCommandHandlerTest()
|
||||||
|
{
|
||||||
|
_requestManager = new Mock<IRequestManager>();
|
||||||
|
_mediator = new Mock<IMediator>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Handler_sends_command_when_order_no_exists()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var fakeGuid = Guid.NewGuid();
|
||||||
|
var fakeOrderCmd = new IdentifiedCommand<CreateOrderCommand, bool>(FakeOrderRequest(), fakeGuid);
|
||||||
|
|
||||||
|
_requestManager.Setup(x => x.ExistAsync(It.IsAny<Guid>()))
|
||||||
|
.Returns(Task.FromResult(false));
|
||||||
|
|
||||||
|
_mediator.Setup(x => x.SendAsync(It.IsAny<IAsyncRequest<bool>>()))
|
||||||
|
.Returns(Task.FromResult(true));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var handler = new IdentifierCommandHandler<CreateOrderCommand, bool>(_mediator.Object, _requestManager.Object);
|
||||||
|
var result = await handler.Handle(fakeOrderCmd);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.True(result);
|
||||||
|
_mediator.Verify(x => x.SendAsync(It.IsAny<IAsyncRequest<bool>>()), Times.Once());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Handler_sends_no_command_when_order_already_exists()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var fakeGuid = Guid.NewGuid();
|
||||||
|
var fakeOrderCmd = new IdentifiedCommand<CreateOrderCommand, bool>(FakeOrderRequest(), fakeGuid);
|
||||||
|
|
||||||
|
_requestManager.Setup(x => x.ExistAsync(It.IsAny<Guid>()))
|
||||||
|
.Returns(Task.FromResult(true));
|
||||||
|
|
||||||
|
_mediator.Setup(x => x.SendAsync(It.IsAny<IAsyncRequest<bool>>()))
|
||||||
|
.Returns(Task.FromResult(true));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var handler = new IdentifierCommandHandler<CreateOrderCommand, bool>(_mediator.Object, _requestManager.Object);
|
||||||
|
var result = await handler.Handle(fakeOrderCmd);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.False(result);
|
||||||
|
_mediator.Verify(x => x.SendAsync(It.IsAny<IAsyncRequest<bool>>()), Times.Never());
|
||||||
|
}
|
||||||
|
|
||||||
|
private CreateOrderCommand FakeOrderRequest(Dictionary<string, object> args = null)
|
||||||
|
{
|
||||||
|
return new CreateOrderCommand(
|
||||||
|
city: args != null && args.ContainsKey("city") ? (string)args["city"] : null,
|
||||||
|
street: args != null && args.ContainsKey("street") ? (string)args["street"] : null,
|
||||||
|
state: args != null && args.ContainsKey("state") ? (string)args["state"] : null,
|
||||||
|
country: args != null && args.ContainsKey("country") ? (string)args["country"] : null,
|
||||||
|
zipcode: args != null && args.ContainsKey("zipcode") ? (string)args["zipcode"] : null,
|
||||||
|
cardNumber: args != null && args.ContainsKey("cardNumber") ? (string)args["cardNumber"] : "1234",
|
||||||
|
cardExpiration: args != null && args.ContainsKey("cardExpiration") ? (DateTime)args["cardExpiration"] : DateTime.MinValue,
|
||||||
|
cardSecurityNumber: args != null && args.ContainsKey("cardSecurityNumber") ? (string)args["cardSecurityNumber"] : "123",
|
||||||
|
cardHolderName: args != null && args.ContainsKey("cardHolderName") ? (string)args["cardHolderName"] : "XXX",
|
||||||
|
cardTypeId: args != null && args.ContainsKey("cardTypeId") ? (int)args["cardTypeId"] : 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,165 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
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.Ordering.Application
|
||||||
|
{
|
||||||
|
public class OrderControllerTest
|
||||||
|
{
|
||||||
|
private readonly Mock<IOrderingService> _orderServiceMock;
|
||||||
|
private readonly Mock<IBasketService> _basketServiceMock;
|
||||||
|
private readonly Mock<IIdentityParser<ApplicationUser>> _identityParserMock;
|
||||||
|
private readonly Mock<HttpContext> _contextMock;
|
||||||
|
|
||||||
|
public OrderControllerTest()
|
||||||
|
{
|
||||||
|
_orderServiceMock = new Mock<IOrderingService>();
|
||||||
|
_basketServiceMock = new Mock<IBasketService>();
|
||||||
|
_identityParserMock = new Mock<IIdentityParser<ApplicationUser>>();
|
||||||
|
_contextMock = new Mock<HttpContext>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Get_order_list_success()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var expectedTotalResults = 1;
|
||||||
|
var fakeOrder = GetFakeOrder();
|
||||||
|
|
||||||
|
_orderServiceMock.Setup(x => x.GetMyOrders(It.IsAny<ApplicationUser>()))
|
||||||
|
.Returns(Task.FromResult(new List<Order>() { fakeOrder }));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var orderController = new OrderController(_orderServiceMock.Object, _basketServiceMock.Object, _identityParserMock.Object);
|
||||||
|
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
||||||
|
var actionResult = await orderController.Index(fakeOrder);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
var viewResult = Assert.IsType<ViewResult>(actionResult);
|
||||||
|
var model = Assert.IsAssignableFrom<List<Order>>(viewResult.ViewData.Model);
|
||||||
|
Assert.Equal(model.Count, expectedTotalResults);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Get_order_detail_success()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fakeOrderId = "12";
|
||||||
|
var fakeOrder = GetFakeOrder();
|
||||||
|
|
||||||
|
_orderServiceMock.Setup(x => x.GetOrder(It.IsAny<ApplicationUser>(), It.IsAny<string>()))
|
||||||
|
.Returns(Task.FromResult(fakeOrder));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var orderController = new OrderController(_orderServiceMock.Object, _basketServiceMock.Object, _identityParserMock.Object);
|
||||||
|
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
||||||
|
var actionResult = await orderController.Detail(fakeOrderId);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
var viewResult = Assert.IsType<ViewResult>(actionResult);
|
||||||
|
Assert.IsAssignableFrom<Order>(viewResult.ViewData.Model);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Get_create_order_success()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fakeBuyerId = "1";
|
||||||
|
var fakeBasket = GetFakeBasket(fakeBuyerId);
|
||||||
|
var fakeOrder = GetFakeOrder();
|
||||||
|
|
||||||
|
_basketServiceMock.Setup(x => x.GetBasket(It.IsAny<ApplicationUser>()))
|
||||||
|
.Returns(Task.FromResult(fakeBasket));
|
||||||
|
|
||||||
|
_basketServiceMock.Setup(x => x.MapBasketToOrder(It.IsAny<BasketModel>()))
|
||||||
|
.Returns(fakeOrder);
|
||||||
|
|
||||||
|
_orderServiceMock.Setup(x => x.MapUserInfoIntoOrder(It.IsAny<ApplicationUser>(), It.IsAny<Order>()))
|
||||||
|
.Returns(fakeOrder);
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var orderController = new OrderController(_orderServiceMock.Object, _basketServiceMock.Object, _identityParserMock.Object);
|
||||||
|
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
||||||
|
var actionResult = await orderController.Create();
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
var viewResult = Assert.IsType<ViewResult>(actionResult);
|
||||||
|
Assert.IsAssignableFrom<Order>(viewResult.ViewData.Model);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Post_create_order_success()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fakeOrder = GetFakeOrder();
|
||||||
|
|
||||||
|
_basketServiceMock.Setup(x => x.CleanBasket(It.IsAny<ApplicationUser>()))
|
||||||
|
.Returns(Task.FromResult(1));
|
||||||
|
|
||||||
|
_orderServiceMock.Setup(x => x.CreateOrder(It.IsAny<Order>()))
|
||||||
|
.Returns(Task.FromResult(1));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var orderController = new OrderController(_orderServiceMock.Object, _basketServiceMock.Object, _identityParserMock.Object);
|
||||||
|
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
||||||
|
var actionResult = await orderController.Create(fakeOrder, "fakeAction");
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
var redirectToActionResult = Assert.IsType<RedirectToActionResult>(actionResult);
|
||||||
|
Assert.Null(redirectToActionResult.ControllerName);
|
||||||
|
Assert.Equal("Index", redirectToActionResult.ActionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Post_create_order_fail()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fakeOrder = GetFakeOrder();
|
||||||
|
|
||||||
|
_basketServiceMock.Setup(x => x.CleanBasket(It.IsAny<ApplicationUser>()))
|
||||||
|
.Returns(Task.FromResult(1));
|
||||||
|
|
||||||
|
_orderServiceMock.Setup(x => x.CreateOrder(It.IsAny<Order>()))
|
||||||
|
.Returns(Task.FromResult(1));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var orderController = new OrderController(_orderServiceMock.Object, _basketServiceMock.Object, _identityParserMock.Object);
|
||||||
|
orderController.ControllerContext.HttpContext = _contextMock.Object;
|
||||||
|
orderController.ModelState.AddModelError("fakeError", "fakeError");
|
||||||
|
var actionResult = await orderController.Create(fakeOrder, "action");
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
var viewResult = Assert.IsType<ViewResult>(actionResult);
|
||||||
|
Assert.IsAssignableFrom<Order>(viewResult.ViewData.Model);
|
||||||
|
}
|
||||||
|
|
||||||
|
private BasketModel GetFakeBasket(string buyerId)
|
||||||
|
{
|
||||||
|
return new BasketModel()
|
||||||
|
{
|
||||||
|
BuyerId = buyerId
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private Order GetFakeOrder()
|
||||||
|
{
|
||||||
|
return new Order()
|
||||||
|
{
|
||||||
|
OrderNumber = "1",
|
||||||
|
CardNumber = "12",
|
||||||
|
CardSecurityNumber = "1212",
|
||||||
|
Status = "Pending",
|
||||||
|
RequestId = Guid.NewGuid(),
|
||||||
|
CardExpiration = DateTime.Now.AddYears(1),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
107
test/Services/UnitTest/Ordering/Application/OrdersWebApiTest.cs
Normal file
107
test/Services/UnitTest/Ordering/Application/OrdersWebApiTest.cs
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
using MediatR;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Queries;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Ordering.API.Controllers;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services;
|
||||||
|
using Moq;
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace UnitTest.Ordering.Application
|
||||||
|
{
|
||||||
|
public class OrdersWebApiTest
|
||||||
|
{
|
||||||
|
private readonly Mock<IMediator> _mediatorMock;
|
||||||
|
private readonly Mock<IOrderQueries> _orderQueriesMock;
|
||||||
|
private readonly Mock<IIdentityService> _identityServiceMock;
|
||||||
|
|
||||||
|
public OrdersWebApiTest()
|
||||||
|
{
|
||||||
|
_mediatorMock = new Mock<IMediator>();
|
||||||
|
_orderQueriesMock = new Mock<IOrderQueries>();
|
||||||
|
_identityServiceMock = new Mock<IIdentityService>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Create_order_with_requestId_success()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_mediatorMock.Setup(x => x.SendAsync(It.IsAny<IdentifiedCommand<CreateOrderCommand, bool>>()))
|
||||||
|
.Returns(Task.FromResult(true));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object);
|
||||||
|
var actionResult = await orderController.CreateOrder(new CreateOrderCommand(), Guid.NewGuid().ToString()) as OkResult;
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Create_order_bad_request()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
_mediatorMock.Setup(x => x.SendAsync(It.IsAny<IdentifiedCommand<CreateOrderCommand, bool>>()))
|
||||||
|
.Returns(Task.FromResult(true));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object);
|
||||||
|
var actionResult = await orderController.CreateOrder(new CreateOrderCommand(), String.Empty) as BadRequestResult;
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.BadRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Get_orders_success()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fakeDynamicResult = new Object();
|
||||||
|
_orderQueriesMock.Setup(x => x.GetOrders())
|
||||||
|
.Returns(Task.FromResult(fakeDynamicResult));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object);
|
||||||
|
var actionResult = await orderController.GetOrders() as OkObjectResult;
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Get_order_success()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fakeOrderId = 123;
|
||||||
|
var fakeDynamicResult = new Object();
|
||||||
|
_orderQueriesMock.Setup(x => x.GetOrder(It.IsAny<int>()))
|
||||||
|
.Returns(Task.FromResult(fakeDynamicResult));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object);
|
||||||
|
var actionResult = await orderController.GetOrder(fakeOrderId) as OkObjectResult;
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Get_cardTypes_success()
|
||||||
|
{
|
||||||
|
//Arrange
|
||||||
|
var fakeDynamicResult = new Object();
|
||||||
|
_orderQueriesMock.Setup(x => x.GetCardTypes())
|
||||||
|
.Returns(Task.FromResult(fakeDynamicResult));
|
||||||
|
|
||||||
|
//Act
|
||||||
|
var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object);
|
||||||
|
var actionResult = await orderController.GetCardTypes() as OkObjectResult;
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,4 @@
|
|||||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
|
||||||
using System;
|
using System;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
@ -13,10 +13,12 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\..\src\Services\Basket\Basket.API\Basket.API.csproj" />
|
||||||
<ProjectReference Include="..\..\..\src\Services\Catalog\Catalog.API\Catalog.API.csproj" />
|
<ProjectReference Include="..\..\..\src\Services\Catalog\Catalog.API\Catalog.API.csproj" />
|
||||||
<ProjectReference Include="..\..\..\src\Services\Ordering\Ordering.API\Ordering.API.csproj" />
|
<ProjectReference Include="..\..\..\src\Services\Ordering\Ordering.API\Ordering.API.csproj" />
|
||||||
<ProjectReference Include="..\..\..\src\Services\Ordering\Ordering.Infrastructure\Ordering.Infrastructure.csproj" />
|
<ProjectReference Include="..\..\..\src\Services\Ordering\Ordering.Infrastructure\Ordering.Infrastructure.csproj" />
|
||||||
<ProjectReference Include="..\..\..\src\Services\Ordering\Ordering.Domain\Ordering.Domain.csproj" />
|
<ProjectReference Include="..\..\..\src\Services\Ordering\Ordering.Domain\Ordering.Domain.csproj" />
|
||||||
|
<ProjectReference Include="..\..\..\src\Web\WebMVC\WebMVC.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user