Browse Source

Complete the test method for creating an order.

pull/99/head
GameBelial 6 years ago
parent
commit
f56b866f36
  1. 66
      modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs
  2. 16
      modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/OrdersApplicationTestBase.cs

66
modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs

@ -1,5 +1,13 @@
using Shouldly; using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using EasyAbp.EShop.Orders.Orders.Dtos;
using EasyAbp.EShop.Products.Products;
using EasyAbp.EShop.Products.Products.Dtos;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Shouldly;
using Xunit; using Xunit;
namespace EasyAbp.EShop.Orders.Orders namespace EasyAbp.EShop.Orders.Orders
@ -8,19 +16,71 @@ namespace EasyAbp.EShop.Orders.Orders
{ {
private readonly IOrderAppService _orderAppService; private readonly IOrderAppService _orderAppService;
private readonly Guid _storeId = Guid.Parse("982C6439-AAC4-CD86-21E0-D89DFE066305");
private readonly Guid _productId = Guid.Parse("309A5529-A42A-9E0F-85FA-879B17B70EA1");
private readonly Guid _productSkuId = Guid.Parse("309A5529-A42A-9E0F-85FA-879B17B70EA2");
public OrderAppServiceTests() public OrderAppServiceTests()
{ {
_orderAppService = GetRequiredService<IOrderAppService>(); _orderAppService = GetRequiredService<IOrderAppService>();
} }
protected override void AfterAddApplication(IServiceCollection services)
{
var productAppService = Substitute.For<IProductAppService>();
productAppService.GetAsync(_productId, _storeId).Returns(Task.FromResult(new ProductDto
{
CreationTime = DateTime.Now,
IsPublished = true,
Id = _productId,
ProductSkus = new List<ProductSkuDto>
{
new ProductSkuDto
{
Id = _productSkuId,
OrderMinQuantity = 0,
OrderMaxQuantity = 100,
AttributeOptionIds = new List<Guid>()
}
},
InventoryStrategy = InventoryStrategy.NoNeed
}));
services.AddTransient(_ => productAppService);
}
[Fact] [Fact]
public async Task Test1() public async Task Should_Create_A_Order()
{ {
// Arrange // Arrange
var createOrderDto = new CreateOrderDto
{
CustomerRemark = "customer remark",
StoreId = _storeId,
OrderLines = new List<CreateOrderLineDto>
{
new CreateOrderLineDto
{
ProductId = _productId,
ProductSkuId = _productSkuId,
Quantity = 10
}
}
};
// Act // Act
var response = await _orderAppService.CreateAsync(createOrderDto);
// Assert // Assert
response.ShouldNotBeNull();
response.CustomerRemark.ShouldBe("customer remark");
UsingDbContext(context =>
{
context.Orders.Count().ShouldBe(1);
var order = context.Orders.First();
order.CustomerRemark.ShouldBe("customer remark");
});
} }
} }
} }

16
modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/OrdersApplicationTestBase.cs

@ -1,10 +1,22 @@
namespace EasyAbp.EShop.Orders using System;
using EasyAbp.EShop.Orders.EntityFrameworkCore;
using EasyAbp.EShop.Products.Products;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
namespace EasyAbp.EShop.Orders
{ {
/* Inherit from this class for your application layer tests. /* Inherit from this class for your application layer tests.
* See SampleAppService_Tests for example. * See SampleAppService_Tests for example.
*/ */
public abstract class OrdersApplicationTestBase : OrdersTestBase<EShopOrdersApplicationTestModule> public abstract class OrdersApplicationTestBase : OrdersTestBase<EShopOrdersApplicationTestModule>
{ {
protected void UsingDbContext(Action<IOrdersDbContext> action)
{
using (var dbContext = GetRequiredService<IOrdersDbContext>())
{
action.Invoke(dbContext);
}
}
} }
} }
Loading…
Cancel
Save