mirror of https://github.com/abpframework/abp.git
5 changed files with 160 additions and 9 deletions
@ -0,0 +1,93 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Application.Dtos; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace ProductManagement |
||||
|
{ |
||||
|
public class ProductAppService_Tests : ProductManagementApplicationTestBase |
||||
|
{ |
||||
|
private readonly IProductAppService _productAppService; |
||||
|
private readonly IProductRepository _productRepository; |
||||
|
private readonly ProductManagementTestData _testData; |
||||
|
|
||||
|
public ProductAppService_Tests() |
||||
|
{ |
||||
|
_productAppService = GetRequiredService<IProductAppService>(); |
||||
|
_productRepository = GetRequiredService<IProductRepository>(); |
||||
|
_testData = GetRequiredService<ProductManagementTestData>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task GetListPagedAsync() |
||||
|
{ |
||||
|
var result = await _productAppService.GetListPagedAsync(new PagedAndSortedResultRequestDto()); |
||||
|
|
||||
|
result.Items.Count.ShouldBeGreaterThan(0); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task GetListAsync() |
||||
|
{ |
||||
|
var result = await _productAppService.GetListAsync(); |
||||
|
|
||||
|
result.Items.Count.ShouldBeGreaterThan(0); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task GetAsync() |
||||
|
{ |
||||
|
var result = await _productAppService.GetAsync(_testData.ProductId1); |
||||
|
|
||||
|
result.ShouldNotBeNull(); |
||||
|
result.Name.ShouldBe(_testData.ProductName1); |
||||
|
result.Code.ShouldBe(_testData.ProductCode1); |
||||
|
result.Price.ShouldBe(_testData.ProductPrice1); |
||||
|
result.StockCount.ShouldBe(_testData.ProductStockCount1); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task CreateAsync() |
||||
|
{ |
||||
|
var result = await _productAppService.CreateAsync(new CreateProductDto() |
||||
|
{ |
||||
|
Code = "Code", |
||||
|
Name = "Name", |
||||
|
Price = 15, |
||||
|
StockCount = 14 |
||||
|
}); |
||||
|
|
||||
|
result.ShouldNotBeNull(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task UpdateAsync() |
||||
|
{ |
||||
|
var result = await _productAppService.UpdateAsync(_testData.ProductId1, new UpdateProductDto() |
||||
|
{ |
||||
|
Code = nameof(Product.Code), |
||||
|
Name = nameof(Product.Name), |
||||
|
Price = 15, |
||||
|
StockCount = 14 |
||||
|
}); |
||||
|
|
||||
|
result.ShouldNotBeNull(); |
||||
|
result.Code.ShouldBe(nameof(Product.Code)); |
||||
|
result.Name.ShouldBe(nameof(Product.Name)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task DeleteAsync() |
||||
|
{ |
||||
|
await _productAppService.DeleteAsync(_testData.ProductId2); |
||||
|
|
||||
|
var result = await _productRepository.GetAsync(_testData.ProductId2); |
||||
|
|
||||
|
result.ShouldBeNull(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
using ProductManagement.EntityFrameworkCore; |
||||
|
|
||||
|
namespace ProductManagement |
||||
|
{ |
||||
|
public abstract class ProductManagementApplicationTestBase : ProductManagementTestBase<ProductManagementApplicationTestModule> |
||||
|
{ |
||||
|
protected virtual void UsingDbContext(Action<IProductManagementDbContext> action) |
||||
|
{ |
||||
|
using (var dbContext = GetRequiredService<IProductManagementDbContext>()) |
||||
|
{ |
||||
|
action.Invoke(dbContext); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected virtual T UsingDbContext<T>(Func<IProductManagementDbContext, T> action) |
||||
|
{ |
||||
|
using (var dbContext = GetRequiredService<IProductManagementDbContext>()) |
||||
|
{ |
||||
|
return action.Invoke(dbContext); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,8 +1,28 @@ |
|||||
using Volo.Abp.DependencyInjection; |
using System; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
namespace ProductManagement |
namespace ProductManagement |
||||
{ |
{ |
||||
public class ProductManagementTestData : ISingletonDependency |
public class ProductManagementTestData : ISingletonDependency |
||||
{ |
{ |
||||
|
public Guid ProductId1 { get; } = Guid.NewGuid(); |
||||
|
|
||||
|
public string ProductCode1 { get; } = "ProductCode1"; |
||||
|
|
||||
|
public string ProductName1 { get; } = "ProductName1"; |
||||
|
|
||||
|
public float ProductPrice1 { get; } = 20; |
||||
|
|
||||
|
public int ProductStockCount1 { get; } = 100; |
||||
|
|
||||
|
public Guid ProductId2 { get; } = Guid.NewGuid(); |
||||
|
|
||||
|
public string ProductCode2 { get; } = "ProductCode2"; |
||||
|
|
||||
|
public string ProductName2 { get; } = "ProductName2"; |
||||
|
|
||||
|
public float ProductPrice2 { get; } = 30; |
||||
|
|
||||
|
public int ProductStockCount2 { get; } = 110; |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -1,24 +1,32 @@ |
|||||
using Volo.Abp.DependencyInjection; |
using System.Threading.Tasks; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
using Volo.Abp.Guids; |
using Volo.Abp.Guids; |
||||
|
using Volo.Abp.Threading; |
||||
|
|
||||
namespace ProductManagement |
namespace ProductManagement |
||||
{ |
{ |
||||
public class ProductManagementTestDataBuilder : ITransientDependency |
public class ProductManagementTestDataBuilder : ITransientDependency |
||||
{ |
{ |
||||
private readonly IGuidGenerator _guidGenerator; |
|
||||
private ProductManagementTestData _testData; |
private ProductManagementTestData _testData; |
||||
|
private readonly ProductManager _productManager; |
||||
|
|
||||
public ProductManagementTestDataBuilder( |
public ProductManagementTestDataBuilder( |
||||
IGuidGenerator guidGenerator, |
ProductManagementTestData testData, |
||||
ProductManagementTestData testData) |
ProductManager productManager) |
||||
{ |
{ |
||||
_guidGenerator = guidGenerator; |
|
||||
_testData = testData; |
_testData = testData; |
||||
|
_productManager = productManager; |
||||
} |
} |
||||
|
|
||||
public void Build() |
public void Build() |
||||
{ |
{ |
||||
|
AsyncHelper.RunSync(BuildAsync); |
||||
|
} |
||||
|
|
||||
|
public async Task BuildAsync() |
||||
|
{ |
||||
|
await _productManager.CreateAsync(_testData.ProductCode1, _testData.ProductName1, _testData.ProductPrice1, _testData.ProductStockCount1); |
||||
|
await _productManager.CreateAsync(_testData.ProductCode2, _testData.ProductName2, _testData.ProductPrice2, _testData.ProductStockCount2); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
Loading…
Reference in new issue