-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
repush to fix some errors in last one
- Loading branch information
1 parent
5a29996
commit e4ba23c
Showing
2 changed files
with
104 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Reflection; | ||
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 Microsoft.Extensions.PlatformAbstractions; | ||
|
||
namespace XUnitTest.WebAPItest.IntegrationTest | ||
{ | ||
/// <summary> | ||
/// A test fixture which hosts the target project (project we wish to test) in an in-memory server. | ||
/// </summary> | ||
/// <typeparam name="TStartup">Target project's startup type</typeparam> | ||
public class TestFixture<TStartup> : IDisposable | ||
{ | ||
private const string SolutionName = "HistoryContest.sln"; | ||
private readonly TestServer _server; | ||
|
||
public TestFixture() | ||
: this(Path.Combine("src")) | ||
{ | ||
} | ||
|
||
protected TestFixture(string solutionRelativeTargetProjectParentDir) | ||
{ | ||
var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly; | ||
var contentRoot = GetProjectPath(solutionRelativeTargetProjectParentDir, startupAssembly); | ||
|
||
var builder = new WebHostBuilder() | ||
.UseContentRoot(contentRoot) | ||
.ConfigureServices(InitializeServices) | ||
.UseEnvironment("Development") | ||
.UseStartup(typeof(TStartup)); | ||
|
||
_server = new TestServer(builder); | ||
|
||
Client = _server.CreateClient(); | ||
Client.BaseAddress = new Uri("http://localhost"); | ||
} | ||
|
||
public HttpClient Client { get; } | ||
|
||
public void Dispose() | ||
{ | ||
Client.Dispose(); | ||
_server.Dispose(); | ||
} | ||
|
||
protected virtual void InitializeServices(IServiceCollection services) | ||
{ | ||
var startupAssembly = typeof(TStartup).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 | ||
var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath; | ||
|
||
// 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, SolutionName)); | ||
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}."); | ||
} | ||
} | ||
} |
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