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
6a754a74
Commit
6a754a74
authored
Jun 06, 2018
by
Scott DePouw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ApiToDoItemsControllerListShould now uses CustomWebApplicationFactory.
parent
6e652f69
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
98 deletions
+10
-98
ApiToDoItemsControllerListShould.cs
...Tests/Integration/Web/ApiToDoItemsControllerListShould.cs
+10
-7
BaseWebTest.cs
tests/CleanArchitecture.Tests/Integration/Web/BaseWebTest.cs
+0
-91
No files found.
tests/CleanArchitecture.Tests/Integration/Web/ApiToDoItemsControllerListShould.cs
View file @
6a754a74
using
CleanArchitecture.Core.Entities
;
using
CleanArchitecture.Web
;
using
Microsoft.AspNetCore.Hosting
;
using
Microsoft.AspNetCore.TestHost
;
using
Newtonsoft.Json
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Linq
;
using
System.Net.Http
;
using
System.Net.Http.Headers
;
using
System.Threading.Tasks
;
using
Xunit
;
namespace
CleanArchitecture.Tests.Integration.Web
{
public
class
ApiToDoItemsControllerList
:
BaseWebTest
public
class
ApiToDoItemsControllerList
:
IClassFixture
<
CustomWebApplicationFactory
<
Startup
>>
{
public
HttpClient
Client
{
get
;
}
public
ApiToDoItemsControllerList
(
CustomWebApplicationFactory
<
Startup
>
factory
)
{
Client
=
factory
.
CreateClient
();
}
[
Fact
]
public
async
Task
ReturnsTwoItems
()
{
var
response
=
await
_c
lient
.
GetAsync
(
"/api/todoitems"
);
var
response
=
await
C
lient
.
GetAsync
(
"/api/todoitems"
);
response
.
EnsureSuccessStatusCode
();
var
stringResponse
=
await
response
.
Content
.
ReadAsStringAsync
();
var
result
=
JsonConvert
.
DeserializeObject
<
IEnumerable
<
ToDoItem
>>(
stringResponse
).
ToList
();
...
...
tests/CleanArchitecture.Tests/Integration/Web/BaseWebTest.cs
deleted
100644 → 0
View file @
6e652f69
using
CleanArchitecture.Web
;
using
Microsoft.AspNetCore.Hosting
;
using
Microsoft.AspNetCore.Mvc.ApplicationParts
;
using
Microsoft.AspNetCore.Mvc.Controllers
;
using
Microsoft.AspNetCore.Mvc.ViewComponents
;
using
Microsoft.AspNetCore.TestHost
;
using
Microsoft.Extensions.DependencyInjection
;
using
System
;
using
System.IO
;
using
System.Net.Http
;
using
System.Reflection
;
namespace
CleanArchitecture.Tests.Integration.Web
{
public
abstract
class
BaseWebTest
{
protected
readonly
HttpClient
_client
;
public
BaseWebTest
()
{
_client
=
GetClient
();
}
protected
HttpClient
GetClient
()
{
var
startupAssembly
=
typeof
(
Startup
).
GetTypeInfo
().
Assembly
;
var
contentRoot
=
GetProjectPath
(
"src"
,
startupAssembly
);
var
builder
=
new
WebHostBuilder
()
.
UseContentRoot
(
contentRoot
)
.
ConfigureServices
(
InitializeServices
)
.
UseStartup
<
Startup
>()
.
UseEnvironment
(
"Testing"
);
// ensure ConfigureTesting is called in Startup
var
server
=
new
TestServer
(
builder
);
var
client
=
server
.
CreateClient
();
return
client
;
}
protected
virtual
void
InitializeServices
(
IServiceCollection
services
)
{
var
startupAssembly
=
typeof
(
Startup
).
GetTypeInfo
().
Assembly
;
// Inject a custom application part manager. Overrides AddMvcCore() because that uses TryAdd().
var
manager
=
new
ApplicationPartManager
();
manager
.
ApplicationParts
.
Add
(
new
AssemblyPart
(
startupAssembly
));
manager
.
FeatureProviders
.
Add
(
new
ControllerFeatureProvider
());
manager
.
FeatureProviders
.
Add
(
new
ViewComponentFeatureProvider
());
services
.
AddSingleton
(
manager
);
}
/// <summary>
/// Gets the full path to the target project path that we wish to test
/// </summary>
/// <param name="solutionRelativePath">
/// The parent directory of the target project.
/// e.g. src, samples, test, or test/Websites
/// </param>
/// <param name="startupAssembly">The target project's assembly.</param>
/// <returns>The full path to the target project.</returns>
private
static
string
GetProjectPath
(
string
solutionRelativePath
,
Assembly
startupAssembly
)
{
// Get name of the target project which we want to test
var
projectName
=
startupAssembly
.
GetName
().
Name
;
// Get currently executing test project path
// AppContext.BaseDirectory or AppDomain.CurrentDomain.BaseDirectory contain the BaseDirectory.
var
applicationBasePath
=
AppContext
.
BaseDirectory
;
// Find the folder which contains the solution file. We then use this information to find the target
// project which we want to test.
var
directoryInfo
=
new
DirectoryInfo
(
applicationBasePath
);
do
{
var
solutionFileInfo
=
new
FileInfo
(
Path
.
Combine
(
directoryInfo
.
FullName
,
"CleanArchitecture.sln"
));
if
(
solutionFileInfo
.
Exists
)
{
return
Path
.
GetFullPath
(
Path
.
Combine
(
directoryInfo
.
FullName
,
solutionRelativePath
,
projectName
));
}
directoryInfo
=
directoryInfo
.
Parent
;
}
while
(
directoryInfo
.
Parent
!=
null
);
throw
new
Exception
(
$"Solution root could not be located using application root
{
applicationBasePath
}
."
);
}
}
}
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