Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CleanArchitecture
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
CleanArchitecture
Commits
d2a1cce0
Commit
d2a1cce0
authored
Jun 06, 2018
by
Scott DePouw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create basic CustomWebApplicationFactory.
parent
88e46889
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
0 deletions
+62
-0
CleanArchitecture.Tests.csproj
tests/CleanArchitecture.Tests/CleanArchitecture.Tests.csproj
+1
-0
CustomWebApplicationFactory.cs
...ture.Tests/Integration/Web/CustomWebApplicationFactory.cs
+61
-0
No files found.
tests/CleanArchitecture.Tests/CleanArchitecture.Tests.csproj
View file @
d2a1cce0
...
@@ -28,6 +28,7 @@
...
@@ -28,6 +28,7 @@
<ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
<PackageReference Include="Moq" Version="4.8.2" />
<PackageReference Include="Moq" Version="4.8.2" />
...
...
tests/CleanArchitecture.Tests/Integration/Web/CustomWebApplicationFactory.cs
0 → 100644
View file @
d2a1cce0
using
CleanArchitecture.Infrastructure.Data
;
using
CleanArchitecture.Web
;
using
Microsoft.AspNetCore.Hosting
;
using
Microsoft.AspNetCore.Mvc.Testing
;
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.Extensions.DependencyInjection
;
using
Microsoft.Extensions.Logging
;
using
System
;
namespace
CleanArchitecture.Tests.Integration.Web
{
public
class
CustomWebApplicationFactory
<
TStartup
>
:
WebApplicationFactory
<
Startup
>
{
protected
override
void
ConfigureWebHost
(
IWebHostBuilder
builder
)
{
builder
.
ConfigureServices
(
services
=>
{
// Create a new service provider.
var
serviceProvider
=
new
ServiceCollection
()
.
AddEntityFrameworkInMemoryDatabase
()
.
BuildServiceProvider
();
// Add a database context (ApplicationDbContext) using an in-memory
// database for testing.
services
.
AddDbContext
<
AppDbContext
>(
options
=>
{
options
.
UseInMemoryDatabase
(
"InMemoryDbForTesting"
);
options
.
UseInternalServiceProvider
(
serviceProvider
);
});
// Build the service provider.
var
sp
=
services
.
BuildServiceProvider
();
// Create a scope to obtain a reference to the database
// context (ApplicationDbContext).
using
(
var
scope
=
sp
.
CreateScope
())
{
var
scopedServices
=
scope
.
ServiceProvider
;
var
db
=
scopedServices
.
GetRequiredService
<
AppDbContext
>();
var
logger
=
scopedServices
.
GetRequiredService
<
ILogger
<
CustomWebApplicationFactory
<
TStartup
>>>();
// Ensure the database is created.
db
.
Database
.
EnsureCreated
();
try
{
// Seed the database with test data.
SeedData
.
PopulateTestData
(
db
);
}
catch
(
Exception
ex
)
{
logger
.
LogError
(
ex
,
$"An error occurred seeding the "
+
"database with test messages. Error: {ex.Message}"
);
}
}
});
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment