-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added AspNet Core integration tests.
- Loading branch information
Showing
10 changed files
with
272 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
test/AbpCompanyName.AbpProjectName.Web.Tests/AbpCompanyName.AbpProjectName.Web.Tests.xproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>5d441612-29ca-4dad-9945-b9de11ce026c</ProjectGuid> | ||
<RootNamespace>AbpCompanyName.AbpProjectName.Web.Tests</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath> | ||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> | ||
</ItemGroup> | ||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
87 changes: 87 additions & 0 deletions
87
test/AbpCompanyName.AbpProjectName.Web.Tests/AbpProjectNameWebTestBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Abp.AspNetCore.TestBase; | ||
using AbpCompanyName.AbpProjectName.EntityFrameworkCore; | ||
using AbpCompanyName.AbpProjectName.Tests.TestDatas; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Serialization; | ||
using Shouldly; | ||
|
||
namespace AbpCompanyName.AbpProjectName.Web.Tests | ||
{ | ||
public abstract class AbpProjectNameWebTestBase : AbpAspNetCoreIntegratedTestBase<Startup> | ||
{ | ||
protected AbpProjectNameWebTestBase() | ||
{ | ||
UsingDbContext(context => new TestDataBuilder(context).Build()); | ||
} | ||
|
||
protected virtual async Task<T> GetResponseAsObjectAsync<T>(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) | ||
{ | ||
var strResponse = await GetResponseAsStringAsync(url, expectedStatusCode); | ||
return JsonConvert.DeserializeObject<T>(strResponse, new JsonSerializerSettings | ||
{ | ||
ContractResolver = new CamelCasePropertyNamesContractResolver() | ||
}); | ||
} | ||
|
||
protected virtual async Task<string> GetResponseAsStringAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) | ||
{ | ||
var response = await GetResponseAsync(url, expectedStatusCode); | ||
return await response.Content.ReadAsStringAsync(); | ||
} | ||
|
||
protected virtual async Task<HttpResponseMessage> GetResponseAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) | ||
{ | ||
var response = await Client.GetAsync(url); | ||
response.StatusCode.ShouldBe(expectedStatusCode); | ||
return response; | ||
} | ||
|
||
protected virtual void UsingDbContext(Action<AbpProjectNameDbContext> action) | ||
{ | ||
using (var context = IocManager.Resolve<AbpProjectNameDbContext>()) | ||
{ | ||
action(context); | ||
context.SaveChanges(); | ||
} | ||
} | ||
|
||
protected virtual T UsingDbContext<T>(Func<AbpProjectNameDbContext, T> func) | ||
{ | ||
T result; | ||
|
||
using (var context = IocManager.Resolve<AbpProjectNameDbContext>()) | ||
{ | ||
result = func(context); | ||
context.SaveChanges(); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
protected virtual async Task UsingDbContextAsync(Func<AbpProjectNameDbContext, Task> action) | ||
{ | ||
using (var context = IocManager.Resolve<AbpProjectNameDbContext>()) | ||
{ | ||
await action(context); | ||
await context.SaveChangesAsync(true); | ||
} | ||
} | ||
|
||
protected virtual async Task<T> UsingDbContextAsync<T>(Func<AbpProjectNameDbContext, Task<T>> func) | ||
{ | ||
T result; | ||
|
||
using (var context = IocManager.Resolve<AbpProjectNameDbContext>()) | ||
{ | ||
result = await func(context); | ||
context.SaveChanges(); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
test/AbpCompanyName.AbpProjectName.Web.Tests/AbpProjectNameWebTestModule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Reflection; | ||
using Abp.AspNetCore.TestBase; | ||
using Abp.Modules; | ||
|
||
namespace AbpCompanyName.AbpProjectName.Web.Tests | ||
{ | ||
[DependsOn( | ||
typeof(AbpProjectNameWebModule), | ||
typeof(AbpAspNetCoreTestBaseModule) | ||
)] | ||
public class AbpProjectNameWebTestModule : AbpModule | ||
{ | ||
public override void Initialize() | ||
{ | ||
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly()); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
test/AbpCompanyName.AbpProjectName.Web.Tests/Controllers/HomeController_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Threading.Tasks; | ||
using AbpCompanyName.AbpProjectName.Web.Controllers; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace AbpCompanyName.AbpProjectName.Web.Tests.Controllers | ||
{ | ||
public class HomeController_Tests: AbpProjectNameWebTestBase | ||
{ | ||
[Fact] | ||
public async Task Test1() | ||
{ | ||
//Act | ||
var response = await GetResponseAsStringAsync( | ||
GetUrl<HomeController>(nameof(HomeController.Index)) | ||
); | ||
|
||
//Assert | ||
|
||
response.ShouldNotBeNullOrEmpty(); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
test/AbpCompanyName.AbpProjectName.Web.Tests/Properties/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("AbpCompanyName.AbpProjectName.Web.Tests")] | ||
[assembly: AssemblyTrademark("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("5d441612-29ca-4dad-9945-b9de11ce026c")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using Abp.AspNetCore; | ||
using Abp.AspNetCore.Mvc; | ||
using Abp.AspNetCore.TestBase; | ||
using Abp.Dependency; | ||
using AbpCompanyName.AbpProjectName.EntityFrameworkCore; | ||
using Castle.MicroKernel.Registration; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace AbpCompanyName.AbpProjectName.Web.Tests | ||
{ | ||
public class Startup | ||
{ | ||
public IServiceProvider ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddEntityFrameworkInMemoryDatabase(); | ||
|
||
services.AddMvc(options => | ||
{ | ||
options.AddAbp(); //Add ABP infrastructure to MVC | ||
}).AddControllersAsServices(); | ||
|
||
//Configure Abp and Dependency Injection | ||
return services.AddAbp(options => | ||
{ | ||
options.SetupTest(testOptions => | ||
{ | ||
testOptions.Modules.Add<AbpProjectNameWebTestModule>(); | ||
}); | ||
}); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | ||
{ | ||
UseInMemoryDb(app.ApplicationServices); | ||
|
||
app.UseAbp(); //Initializes ABP framework. | ||
|
||
app.UseExceptionHandler("/Error"); | ||
|
||
app.UseStaticFiles(); | ||
|
||
app.UseMvc(routes => | ||
{ | ||
routes.MapRoute( | ||
name: "default", | ||
template: "{controller=Home}/{action=Index}/{id?}" | ||
); | ||
}); | ||
} | ||
|
||
private void UseInMemoryDb(IServiceProvider serviceProvider) | ||
{ | ||
var builder = new DbContextOptionsBuilder<AbpProjectNameDbContext>(); | ||
builder.UseInMemoryDatabase().UseInternalServiceProvider(serviceProvider); | ||
var options = builder.Options; | ||
|
||
var iocManager = serviceProvider.GetRequiredService<IIocManager>(); | ||
|
||
iocManager.IocContainer | ||
.Register( | ||
Component.For<DbContextOptions<AbpProjectNameDbContext>>() | ||
.Instance(options) | ||
.LifestyleSingleton() | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"version": "1.0.0.0-*", | ||
|
||
"testRunner": "xunit", | ||
|
||
"dependencies": { | ||
"Abp.AspNetCore.TestBase": "0.9.6", | ||
"AbpCompanyName.AbpProjectName.Tests": "1.0.0.0-*", | ||
"AbpCompanyName.AbpProjectName.Web": "1.0.0-*", | ||
"Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final" | ||
}, | ||
|
||
"frameworks": { | ||
"net461": { | ||
"Microsoft.NETCore.App": "1.0.0-rc2-3002702" | ||
} | ||
} | ||
} |