Skip to content

Commit

Permalink
Added AspNet Core integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed Jun 22, 2016
1 parent 3f81291 commit e1b2f5b
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 4 deletions.
7 changes: 7 additions & 0 deletions AbpCompanyName.AbpProjectName.sln
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F10AA149-2
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AbpCompanyName.AbpProjectName.Tests", "test\AbpCompanyName.AbpProjectName.Tests\AbpCompanyName.AbpProjectName.Tests.xproj", "{0D4C5D00-C144-4213-A007-4B8944113AB1}"
EndProject
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -48,6 +50,10 @@ Global
{0D4C5D00-C144-4213-A007-4B8944113AB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0D4C5D00-C144-4213-A007-4B8944113AB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0D4C5D00-C144-4213-A007-4B8944113AB1}.Release|Any CPU.Build.0 = Release|Any CPU
{5D441612-29CA-4DAD-9945-B9DE11CE026C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5D441612-29CA-4DAD-9945-B9DE11CE026C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5D441612-29CA-4DAD-9945-B9DE11CE026C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5D441612-29CA-4DAD-9945-B9DE11CE026C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -58,5 +64,6 @@ Global
{3870C648-4AEA-4B85-BA3F-F2F63B96136A} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
{DC780BC4-4EAC-4C63-9052-6F3169A59FA4} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC}
{0D4C5D00-C144-4213-A007-4B8944113AB1} = {F10AA149-2626-486E-85BB-9CD5365F3016}
{5D441612-29CA-4DAD-9945-B9DE11CE026C} = {F10AA149-2626-486E-85BB-9CD5365F3016}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using Abp.AspNetCore.Mvc.Controllers;
using Abp.Json;
using Abp.UI;
using AbpCompanyName.AbpProjectName.Products;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -18,6 +19,7 @@ public HomeController(IProductAppService productAppService)
public async Task<ActionResult> Index()
{
var model = await _productAppService.GetAllProducts();
//return Content(model.ToJsonString());
return View(model);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected override void AddModules(ITypeList<AbpModule> modules)
modules.Add<AbpProjectNameTestModule>();
}

public void UsingDbContext(Action<AbpProjectNameDbContext> action)
protected virtual void UsingDbContext(Action<AbpProjectNameDbContext> action)
{
using (var context = LocalIocManager.Resolve<AbpProjectNameDbContext>())
{
Expand All @@ -30,7 +30,7 @@ public void UsingDbContext(Action<AbpProjectNameDbContext> action)
}
}

public T UsingDbContext<T>(Func<AbpProjectNameDbContext, T> func)
protected virtual T UsingDbContext<T>(Func<AbpProjectNameDbContext, T> func)
{
T result;

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

public async Task UsingDbContextAsync(Func<AbpProjectNameDbContext, Task> action)
protected virtual async Task UsingDbContextAsync(Func<AbpProjectNameDbContext, Task> action)
{
using (var context = LocalIocManager.Resolve<AbpProjectNameDbContext>())
{
Expand All @@ -52,7 +52,7 @@ public async Task UsingDbContextAsync(Func<AbpProjectNameDbContext, Task> action
}
}

public async Task<T> UsingDbContextAsync<T>(Func<AbpProjectNameDbContext, Task<T>> func)
protected virtual async Task<T> UsingDbContextAsync<T>(Func<AbpProjectNameDbContext, Task<T>> func)
{
T result;

Expand Down
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>
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;
}
}
}
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());
}
}
}
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();
}
}
}
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")]
72 changes: 72 additions & 0 deletions test/AbpCompanyName.AbpProjectName.Web.Tests/Startup.cs
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()
);
}
}
}
18 changes: 18 additions & 0 deletions test/AbpCompanyName.AbpProjectName.Web.Tests/project.json
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"
}
}
}

0 comments on commit e1b2f5b

Please sign in to comment.