Skip to content

Instantly share code, notes, and snippets.

@aramkoukia
Created April 16, 2018 14:56
Show Gist options
  • Save aramkoukia/5f4d458272b8051a009307dfcd96526c to your computer and use it in GitHub Desktop.
Save aramkoukia/5f4d458272b8051a009307dfcd96526c to your computer and use it in GitHub Desktop.
Test Server Fixture .Net Core
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