Skip to content

Commit

Permalink
repush to fix some errors in last one
Browse files Browse the repository at this point in the history
  • Loading branch information
HangyuanYang committed Sep 4, 2017
1 parent 5a29996 commit e4ba23c
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
103 changes: 103 additions & 0 deletions HistoryContest.Test/WebAPITest/TestFixture.cs
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}.");
}
}
}
2 changes: 1 addition & 1 deletion HistoryContest.Test/WebAPITest/WebAPITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using HistoryContest.Server.Controllers.APIs;
using Xunit;

namespace XUnitTest.IntegrationTest.WebAPItest
namespace XUnitTest.WebAPItest.IntegrationTest
{
public class ApiIdeasControllerTests : IClassFixture<TestFixture<HistoryContest.Server.Startup>>
{
Expand Down

0 comments on commit e4ba23c

Please sign in to comment.