Created
April 16, 2018 14:56
-
-
Save aramkoukia/5f4d458272b8051a009307dfcd96526c to your computer and use it in GitHub Desktop.
Test Server Fixture .Net Core
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
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.TestHost; | |
using Microsoft.Extensions.PlatformAbstractions; | |
using System; | |
using System.IO; | |
using System.Net.Http; | |
namespace Product.CommandService.IntegrationTests | |
{ | |
public class TestServerFixture : IDisposable | |
{ | |
private readonly TestServer _testServer; | |
public HttpClient Client { get; } | |
public TestServerFixture() | |
{ | |
var builder = new WebHostBuilder() | |
.UseContentRoot(GetContentRootPath()) | |
.UseEnvironment("Development") | |
.UseStartup<Startup>(); // Uses Start up class from your API Host project to configure the test server | |
_testServer = new TestServer(builder); | |
Client = _testServer.CreateClient(); | |
} | |
private string GetContentRootPath() | |
{ | |
var testProjectPath = PlatformServices.Default.Application.ApplicationBasePath; | |
var relativePathToHostProject = @"..\..\..\..\..\..\Product.CommandService"; | |
return Path.Combine(testProjectPath, relativePathToHostProject); | |
} | |
public void Dispose() | |
{ | |
Client.Dispose(); | |
_testServer.Dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment