Skip to content

Commit 42e2cdf

Browse files
committed
Added unit test project and a simple test code.
1 parent 809ea36 commit 42e2cdf

File tree

13 files changed

+263
-4
lines changed

13 files changed

+263
-4
lines changed

AbpCompanyName.AbpProjectName.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AbpCompanyName.AbpProjectNa
1818
EndProject
1919
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AbpCompanyName.AbpProjectName.EntityFrameworkCore", "src\AbpCompanyName.AbpProjectName.EntityFrameworkCore\AbpCompanyName.AbpProjectName.EntityFrameworkCore.xproj", "{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}"
2020
EndProject
21+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F10AA149-2626-486E-85BB-9CD5365F3016}"
22+
EndProject
23+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AbpCompanyName.AbpProjectName.Tests", "test\AbpCompanyName.AbpProjectName.Tests\AbpCompanyName.AbpProjectName.Tests.xproj", "{0D4C5D00-C144-4213-A007-4B8944113AB1}"
24+
EndProject
2125
Global
2226
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2327
Debug|Any CPU = Debug|Any CPU
@@ -40,6 +44,10 @@ Global
4044
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
4145
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
4246
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4}.Release|Any CPU.Build.0 = Release|Any CPU
47+
{0D4C5D00-C144-4213-A007-4B8944113AB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
48+
{0D4C5D00-C144-4213-A007-4B8944113AB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
49+
{0D4C5D00-C144-4213-A007-4B8944113AB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
50+
{0D4C5D00-C144-4213-A007-4B8944113AB1}.Release|Any CPU.Build.0 = Release|Any CPU
4351
EndGlobalSection
4452
GlobalSection(SolutionProperties) = preSolution
4553
HideSolutionNode = FALSE
@@ -49,5 +57,6 @@ Global
4957
{0FA75A5B-AB83-4FD0-B545-279774C01E87} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
5058
{3870C648-4AEA-4B85-BA3F-F2F63B96136A} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
5159
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
60+
{0D4C5D00-C144-4213-A007-4B8944113AB1} = {F10AA149-2626-486E-85BB-9CD5365F3016}
5261
EndGlobalSection
5362
EndGlobal

src/AbpCompanyName.AbpProjectName.Application/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88

99
"frameworks": {
10-
"net452": {
10+
"net461": {
1111
}
1212
}
1313
}

src/AbpCompanyName.AbpProjectName.Core/Products/Product.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.ComponentModel.DataAnnotations;
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
23
using Abp.Domain.Entities.Auditing;
4+
using Abp.Extensions;
35

46
namespace AbpCompanyName.AbpProjectName.Products
57
{
@@ -12,5 +14,21 @@ public class Product : AuditedEntity
1214
public string Name { get; set; }
1315

1416
public float? Price { get; set; }
17+
18+
public Product()
19+
{
20+
21+
}
22+
23+
public Product(string name, float? price = null)
24+
{
25+
if (name.IsNullOrEmpty())
26+
{
27+
throw new ArgumentException($"nameof(name) can not be null or empty", nameof(name));
28+
}
29+
30+
Name = name;
31+
Price = price;
32+
}
1533
}
1634
}

src/AbpCompanyName.AbpProjectName.Core/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77

88
"frameworks": {
9-
"net452": {
9+
"net461": {
1010
}
1111
},
1212

src/AbpCompanyName.AbpProjectName.EntityFrameworkCore/EntityFrameworkCore/AbpProjectNameDbContextFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace AbpCompanyName.AbpProjectName.EntityFrameworkCore
55
{
6+
/* This class is needed to run "dotnet ef ..." commands from command line.
7+
*/
68
public class AbpProjectNameDbContextFactory : IDbContextFactory<AbpProjectNameDbContext>
79
{
810
public AbpProjectNameDbContext Create()

src/AbpCompanyName.AbpProjectName.EntityFrameworkCore/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727

2828
"frameworks": {
29-
"net452": {
29+
"net461": {
3030
"imports": "portable-net451+win8",
3131
"Microsoft.NETCore.App": "1.0.0-rc2-3002702"
3232
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>0d4c5d00-c144-4213-a007-4b8944113ab1</ProjectGuid>
10+
<RootNamespace>AbpCompanyName.AbpProjectName.Tests</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
13+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
14+
</PropertyGroup>
15+
<PropertyGroup>
16+
<SchemaVersion>2.0</SchemaVersion>
17+
</PropertyGroup>
18+
<ItemGroup>
19+
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
20+
</ItemGroup>
21+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
22+
</Project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Abp.Collections;
4+
using Abp.Modules;
5+
using Abp.TestBase;
6+
using AbpCompanyName.AbpProjectName.EntityFrameworkCore;
7+
using AbpCompanyName.AbpProjectName.Tests.TestDatas;
8+
9+
namespace AbpCompanyName.AbpProjectName.Tests
10+
{
11+
public class AbpProjectNameTestBase : AbpIntegratedTestBase
12+
{
13+
public AbpProjectNameTestBase()
14+
{
15+
UsingDbContext(context => new TestDataBuilder(context).Build());
16+
}
17+
18+
protected override void AddModules(ITypeList<AbpModule> modules)
19+
{
20+
base.AddModules(modules);
21+
modules.Add<AbpProjectNameTestModule>();
22+
}
23+
24+
public void UsingDbContext(Action<AbpProjectNameDbContext> action)
25+
{
26+
using (var context = LocalIocManager.Resolve<AbpProjectNameDbContext>())
27+
{
28+
action(context);
29+
context.SaveChanges();
30+
}
31+
}
32+
33+
public T UsingDbContext<T>(Func<AbpProjectNameDbContext, T> func)
34+
{
35+
T result;
36+
37+
using (var context = LocalIocManager.Resolve<AbpProjectNameDbContext>())
38+
{
39+
result = func(context);
40+
context.SaveChanges();
41+
}
42+
43+
return result;
44+
}
45+
46+
public async Task UsingDbContextAsync(Func<AbpProjectNameDbContext, Task> action)
47+
{
48+
using (var context = LocalIocManager.Resolve<AbpProjectNameDbContext>())
49+
{
50+
await action(context);
51+
await context.SaveChangesAsync(true);
52+
}
53+
}
54+
55+
public async Task<T> UsingDbContextAsync<T>(Func<AbpProjectNameDbContext, Task<T>> func)
56+
{
57+
T result;
58+
59+
using (var context = LocalIocManager.Resolve<AbpProjectNameDbContext>())
60+
{
61+
result = await func(context);
62+
context.SaveChanges();
63+
}
64+
65+
return result;
66+
}
67+
}
68+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Reflection;
2+
using Abp.Modules;
3+
using AbpCompanyName.AbpProjectName.EntityFrameworkCore;
4+
using Castle.MicroKernel.Registration;
5+
using Castle.Windsor.MsDependencyInjection;
6+
using Microsoft.EntityFrameworkCore;
7+
using Microsoft.Extensions.DependencyInjection;
8+
9+
namespace AbpCompanyName.AbpProjectName.Tests
10+
{
11+
[DependsOn(
12+
typeof(AbpProjectNameApplicationModule),
13+
typeof(AbpProjectNameEntityFrameworkCoreModule)
14+
)]
15+
public class AbpProjectNameTestModule : AbpModule
16+
{
17+
public override void PreInitialize()
18+
{
19+
var services = new ServiceCollection()
20+
.AddEntityFrameworkInMemoryDatabase();
21+
22+
var serviceProvider = WindsorRegistrationHelper.CreateServiceProvider(
23+
IocManager.IocContainer,
24+
services
25+
);
26+
27+
var builder = new DbContextOptionsBuilder<AbpProjectNameDbContext>();
28+
builder.UseInMemoryDatabase()
29+
.UseInternalServiceProvider(serviceProvider);
30+
31+
var options = builder.Options;
32+
33+
IocManager.IocContainer.Register(
34+
Component.For<DbContextOptions<AbpProjectNameDbContext>>().Instance(options).LifestyleSingleton()
35+
);
36+
}
37+
38+
public override void Initialize()
39+
{
40+
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
41+
}
42+
}
43+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Threading.Tasks;
2+
using AbpCompanyName.AbpProjectName.Products;
3+
using Shouldly;
4+
using Xunit;
5+
6+
namespace AbpCompanyName.AbpProjectName.Tests.Products
7+
{
8+
public class ProductAppService_Tests : AbpProjectNameTestBase
9+
{
10+
private readonly IProductAppService _productAppService;
11+
12+
public ProductAppService_Tests()
13+
{
14+
_productAppService = Resolve<IProductAppService>();
15+
}
16+
17+
[Fact]
18+
public async Task GetAllProducts_Should_Return_Products()
19+
{
20+
//Act
21+
var output = await _productAppService.GetAllProducts();
22+
23+
//Assert
24+
output.Items.Count.ShouldBeGreaterThan(0);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)