Skip to content

Commit e1b2f5b

Browse files
committed
Added AspNet Core integration tests.
1 parent 3f81291 commit e1b2f5b

File tree

10 files changed

+272
-4
lines changed

10 files changed

+272
-4
lines changed

AbpCompanyName.AbpProjectName.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F10AA149-2
2222
EndProject
2323
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AbpCompanyName.AbpProjectName.Tests", "test\AbpCompanyName.AbpProjectName.Tests\AbpCompanyName.AbpProjectName.Tests.xproj", "{0D4C5D00-C144-4213-A007-4B8944113AB1}"
2424
EndProject
25+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AbpCompanyName.AbpProjectName.Web.Tests", "test\AbpCompanyName.AbpProjectName.Web.Tests\AbpCompanyName.AbpProjectName.Web.Tests.xproj", "{5D441612-29CA-4DAD-9945-B9DE11CE026C}"
26+
EndProject
2527
Global
2628
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2729
Debug|Any CPU = Debug|Any CPU
@@ -48,6 +50,10 @@ Global
4850
{0D4C5D00-C144-4213-A007-4B8944113AB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
4951
{0D4C5D00-C144-4213-A007-4B8944113AB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
5052
{0D4C5D00-C144-4213-A007-4B8944113AB1}.Release|Any CPU.Build.0 = Release|Any CPU
53+
{5D441612-29CA-4DAD-9945-B9DE11CE026C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
54+
{5D441612-29CA-4DAD-9945-B9DE11CE026C}.Debug|Any CPU.Build.0 = Debug|Any CPU
55+
{5D441612-29CA-4DAD-9945-B9DE11CE026C}.Release|Any CPU.ActiveCfg = Release|Any CPU
56+
{5D441612-29CA-4DAD-9945-B9DE11CE026C}.Release|Any CPU.Build.0 = Release|Any CPU
5157
EndGlobalSection
5258
GlobalSection(SolutionProperties) = preSolution
5359
HideSolutionNode = FALSE
@@ -58,5 +64,6 @@ Global
5864
{3870C648-4AEA-4B85-BA3F-F2F63B96136A} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
5965
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
6066
{0D4C5D00-C144-4213-A007-4B8944113AB1} = {F10AA149-2626-486E-85BB-9CD5365F3016}
67+
{5D441612-29CA-4DAD-9945-B9DE11CE026C} = {F10AA149-2626-486E-85BB-9CD5365F3016}
6168
EndGlobalSection
6269
EndGlobal

src/AbpCompanyName.AbpProjectName.Web/Controllers/HomeController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Threading.Tasks;
22
using Abp.AspNetCore.Mvc.Controllers;
3+
using Abp.Json;
34
using Abp.UI;
45
using AbpCompanyName.AbpProjectName.Products;
56
using Microsoft.AspNetCore.Mvc;
@@ -18,6 +19,7 @@ public HomeController(IProductAppService productAppService)
1819
public async Task<ActionResult> Index()
1920
{
2021
var model = await _productAppService.GetAllProducts();
22+
//return Content(model.ToJsonString());
2123
return View(model);
2224
}
2325

test/AbpCompanyName.AbpProjectName.Tests/AbpProjectNameTestBase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected override void AddModules(ITypeList<AbpModule> modules)
2121
modules.Add<AbpProjectNameTestModule>();
2222
}
2323

24-
public void UsingDbContext(Action<AbpProjectNameDbContext> action)
24+
protected virtual void UsingDbContext(Action<AbpProjectNameDbContext> action)
2525
{
2626
using (var context = LocalIocManager.Resolve<AbpProjectNameDbContext>())
2727
{
@@ -30,7 +30,7 @@ public void UsingDbContext(Action<AbpProjectNameDbContext> action)
3030
}
3131
}
3232

33-
public T UsingDbContext<T>(Func<AbpProjectNameDbContext, T> func)
33+
protected virtual T UsingDbContext<T>(Func<AbpProjectNameDbContext, T> func)
3434
{
3535
T result;
3636

@@ -43,7 +43,7 @@ public T UsingDbContext<T>(Func<AbpProjectNameDbContext, T> func)
4343
return result;
4444
}
4545

46-
public async Task UsingDbContextAsync(Func<AbpProjectNameDbContext, Task> action)
46+
protected virtual async Task UsingDbContextAsync(Func<AbpProjectNameDbContext, Task> action)
4747
{
4848
using (var context = LocalIocManager.Resolve<AbpProjectNameDbContext>())
4949
{
@@ -52,7 +52,7 @@ public async Task UsingDbContextAsync(Func<AbpProjectNameDbContext, Task> action
5252
}
5353
}
5454

55-
public async Task<T> UsingDbContextAsync<T>(Func<AbpProjectNameDbContext, Task<T>> func)
55+
protected virtual async Task<T> UsingDbContextAsync<T>(Func<AbpProjectNameDbContext, Task<T>> func)
5656
{
5757
T result;
5858

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>5d441612-29ca-4dad-9945-b9de11ce026c</ProjectGuid>
10+
<RootNamespace>AbpCompanyName.AbpProjectName.Web.Tests</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
13+
<TargetFrameworkVersion>v4.6.1</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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.Net;
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
using Abp.AspNetCore.TestBase;
6+
using AbpCompanyName.AbpProjectName.EntityFrameworkCore;
7+
using AbpCompanyName.AbpProjectName.Tests.TestDatas;
8+
using Newtonsoft.Json;
9+
using Newtonsoft.Json.Serialization;
10+
using Shouldly;
11+
12+
namespace AbpCompanyName.AbpProjectName.Web.Tests
13+
{
14+
public abstract class AbpProjectNameWebTestBase : AbpAspNetCoreIntegratedTestBase<Startup>
15+
{
16+
protected AbpProjectNameWebTestBase()
17+
{
18+
UsingDbContext(context => new TestDataBuilder(context).Build());
19+
}
20+
21+
protected virtual async Task<T> GetResponseAsObjectAsync<T>(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK)
22+
{
23+
var strResponse = await GetResponseAsStringAsync(url, expectedStatusCode);
24+
return JsonConvert.DeserializeObject<T>(strResponse, new JsonSerializerSettings
25+
{
26+
ContractResolver = new CamelCasePropertyNamesContractResolver()
27+
});
28+
}
29+
30+
protected virtual async Task<string> GetResponseAsStringAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK)
31+
{
32+
var response = await GetResponseAsync(url, expectedStatusCode);
33+
return await response.Content.ReadAsStringAsync();
34+
}
35+
36+
protected virtual async Task<HttpResponseMessage> GetResponseAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK)
37+
{
38+
var response = await Client.GetAsync(url);
39+
response.StatusCode.ShouldBe(expectedStatusCode);
40+
return response;
41+
}
42+
43+
protected virtual void UsingDbContext(Action<AbpProjectNameDbContext> action)
44+
{
45+
using (var context = IocManager.Resolve<AbpProjectNameDbContext>())
46+
{
47+
action(context);
48+
context.SaveChanges();
49+
}
50+
}
51+
52+
protected virtual T UsingDbContext<T>(Func<AbpProjectNameDbContext, T> func)
53+
{
54+
T result;
55+
56+
using (var context = IocManager.Resolve<AbpProjectNameDbContext>())
57+
{
58+
result = func(context);
59+
context.SaveChanges();
60+
}
61+
62+
return result;
63+
}
64+
65+
protected virtual async Task UsingDbContextAsync(Func<AbpProjectNameDbContext, Task> action)
66+
{
67+
using (var context = IocManager.Resolve<AbpProjectNameDbContext>())
68+
{
69+
await action(context);
70+
await context.SaveChangesAsync(true);
71+
}
72+
}
73+
74+
protected virtual async Task<T> UsingDbContextAsync<T>(Func<AbpProjectNameDbContext, Task<T>> func)
75+
{
76+
T result;
77+
78+
using (var context = IocManager.Resolve<AbpProjectNameDbContext>())
79+
{
80+
result = await func(context);
81+
context.SaveChanges();
82+
}
83+
84+
return result;
85+
}
86+
}
87+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Reflection;
2+
using Abp.AspNetCore.TestBase;
3+
using Abp.Modules;
4+
5+
namespace AbpCompanyName.AbpProjectName.Web.Tests
6+
{
7+
[DependsOn(
8+
typeof(AbpProjectNameWebModule),
9+
typeof(AbpAspNetCoreTestBaseModule)
10+
)]
11+
public class AbpProjectNameWebTestModule : AbpModule
12+
{
13+
public override void Initialize()
14+
{
15+
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
16+
}
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Threading.Tasks;
2+
using AbpCompanyName.AbpProjectName.Web.Controllers;
3+
using Shouldly;
4+
using Xunit;
5+
6+
namespace AbpCompanyName.AbpProjectName.Web.Tests.Controllers
7+
{
8+
public class HomeController_Tests: AbpProjectNameWebTestBase
9+
{
10+
[Fact]
11+
public async Task Test1()
12+
{
13+
//Act
14+
var response = await GetResponseAsStringAsync(
15+
GetUrl<HomeController>(nameof(HomeController.Index))
16+
);
17+
18+
//Assert
19+
20+
response.ShouldNotBeNullOrEmpty();
21+
}
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyConfiguration("")]
9+
[assembly: AssemblyCompany("")]
10+
[assembly: AssemblyProduct("AbpCompanyName.AbpProjectName.Web.Tests")]
11+
[assembly: AssemblyTrademark("")]
12+
13+
// Setting ComVisible to false makes the types in this assembly not visible
14+
// to COM components. If you need to access a type in this assembly from
15+
// COM, set the ComVisible attribute to true on that type.
16+
[assembly: ComVisible(false)]
17+
18+
// The following GUID is for the ID of the typelib if this project is exposed to COM
19+
[assembly: Guid("5d441612-29ca-4dad-9945-b9de11ce026c")]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using Abp.AspNetCore;
3+
using Abp.AspNetCore.Mvc;
4+
using Abp.AspNetCore.TestBase;
5+
using Abp.Dependency;
6+
using AbpCompanyName.AbpProjectName.EntityFrameworkCore;
7+
using Castle.MicroKernel.Registration;
8+
using Microsoft.AspNetCore.Builder;
9+
using Microsoft.AspNetCore.Hosting;
10+
using Microsoft.EntityFrameworkCore;
11+
using Microsoft.Extensions.DependencyInjection;
12+
using Microsoft.Extensions.Logging;
13+
14+
namespace AbpCompanyName.AbpProjectName.Web.Tests
15+
{
16+
public class Startup
17+
{
18+
public IServiceProvider ConfigureServices(IServiceCollection services)
19+
{
20+
services.AddEntityFrameworkInMemoryDatabase();
21+
22+
services.AddMvc(options =>
23+
{
24+
options.AddAbp(); //Add ABP infrastructure to MVC
25+
}).AddControllersAsServices();
26+
27+
//Configure Abp and Dependency Injection
28+
return services.AddAbp(options =>
29+
{
30+
options.SetupTest(testOptions =>
31+
{
32+
testOptions.Modules.Add<AbpProjectNameWebTestModule>();
33+
});
34+
});
35+
}
36+
37+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
38+
{
39+
UseInMemoryDb(app.ApplicationServices);
40+
41+
app.UseAbp(); //Initializes ABP framework.
42+
43+
app.UseExceptionHandler("/Error");
44+
45+
app.UseStaticFiles();
46+
47+
app.UseMvc(routes =>
48+
{
49+
routes.MapRoute(
50+
name: "default",
51+
template: "{controller=Home}/{action=Index}/{id?}"
52+
);
53+
});
54+
}
55+
56+
private void UseInMemoryDb(IServiceProvider serviceProvider)
57+
{
58+
var builder = new DbContextOptionsBuilder<AbpProjectNameDbContext>();
59+
builder.UseInMemoryDatabase().UseInternalServiceProvider(serviceProvider);
60+
var options = builder.Options;
61+
62+
var iocManager = serviceProvider.GetRequiredService<IIocManager>();
63+
64+
iocManager.IocContainer
65+
.Register(
66+
Component.For<DbContextOptions<AbpProjectNameDbContext>>()
67+
.Instance(options)
68+
.LifestyleSingleton()
69+
);
70+
}
71+
}
72+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": "1.0.0.0-*",
3+
4+
"testRunner": "xunit",
5+
6+
"dependencies": {
7+
"Abp.AspNetCore.TestBase": "0.9.6",
8+
"AbpCompanyName.AbpProjectName.Tests": "1.0.0.0-*",
9+
"AbpCompanyName.AbpProjectName.Web": "1.0.0-*",
10+
"Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final"
11+
},
12+
13+
"frameworks": {
14+
"net461": {
15+
"Microsoft.NETCore.App": "1.0.0-rc2-3002702"
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)