9

The following code works as expected and is really similar to the samples found on the asp site:

public abstract class BaseResourceTests : IDisposable
{
    private readonly TestServer _server;

    public HttpClient HttpClient { get; }

    protected BaseResourceTests(string resourceVersion)
    {
        var hostBulider = new WebHostBuilder()
            .UseStartup<Startup>();

        _server = new TestServer(hostBulider);

        HttpClient = _server.CreateClient();
        HttpClient.BaseAddress = new Uri("http://localhost:5000");
    }

    public virtual void Dispose()
    {
        HttpClient.Dispose();
        _server.Dispose();
    }
}

public class EndpointTests : BaseResourceTests
{
    public EndpointTests()
        : base(Resource.VersionHeader)
    {
    }

    [Fact]
    public async Task Post_BodyHasValidDto_ReturnsCreated()
    {
        var dto = new Dto { Name = "someDto" };

        var response = await HttpClient.PostAsJsonAsync("/api/someEndpoint", dto);

        Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.Created);
    }
}

My test works fine it reaches the action in the controller just fine. However in real life the tests are a little more complicated I need to add some services and custom test configuration.

In order to do this I create a TestStartup that inherits from my original project Startup class:

public class TestStartup : Startup
{
    public TestStartup(IHostingEnvironment env) : base(env)
    {
    }

    public override void ConfigureServices(IServiceCollection services)
    {
        base.ConfigureServices(services);
    }

    public override void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        base.Configure(app, env, loggerFactory);
    }
}

As you can see it only delegates to the base class, so now I change the builder to use my TestStartup code:

    protected BaseResourceTests(string resourceVersion)
    {
        var hostBulider = new WebHostBuilder()
            .UseStartup<TestStartup>();

        _server = new TestServer(hostBulider);

        HttpClient = _server.CreateClient();
        HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(resourceVersion));
        HttpClient.BaseAddress = new Uri("http://localhost:5000");
    }

And the tests now fail with with Http NotFound exception. When debugging, it turns out that the action in the controller is no longer hit by the test code.

My guess is that somehow mvc does not detect the routes if the startup file is a different assembly then the controllers, how can I work around this?

2
  • try to change '_server = new TestServer(hostBulider);' to _server = TestServer.CreateBuilder().UseStartup<TestStartup>();
    – Set
    Jun 21, 2016 at 15:40
  • There is no static method CreateBuilder on the TestServer class, I think it used to be in rc1
    – Calin
    Jun 23, 2016 at 16:31

4 Answers 4

2

One more additional change that I had to do to get an overridden startup class to work is to set the ApplicationName in the IHostingEnvironment object to the actual name of the web project.

public TestStartup(IHostingEnvironment env) : base(env)
        {
            env.ApplicationName = "Demo.Web";
        }

This is required when the TestStartup is in a different assembly and overrides the original Startup class. UseContentRoot was still required in my case.

If the name is not set, I always got a 404 not found.

1

I have written a post about doing this sort of integration testing with xUnit here: http://andrewlock.net/introduction-to-integration-testing-with-xunit-and-testserver-in-asp-net-core.


The approach I took was to create a Fixture class which can be injected into your test class.

The Fixture:

public class TestFixture<TStartup> : IDisposable where TStartup : class  
{
    private readonly TestServer _server;

    public TestFixture()
    {
        var builder = new WebHostBuilder().UseStartup<TStartup>();
        _server = new TestServer(builder);

        Client = _server.CreateClient();
        Client.BaseAddress = new Uri("http://localhost:5000");
    }

    public HttpClient Client { get; }

    public void Dispose()
    {
        Client.Dispose();
        _server.Dispose();
    }
}

The test class then implements IClassFixture<TestFixture<TStartup>>:

public class MiddlewareIntegrationTests : IClassFixture<TestFixture<SystemUnderTest.Startup>>  
{
    public MiddlewareIntegrationTests(TestFixture<SystemUnderTest.Startup> fixture)
    {
        Client = fixture.Client;
    }

    public HttpClient Client { get; }

    [Fact]
    public async Task AllMethods_RemovesServerHeader(string method)
    {
       // Arrange
       var request = new HttpRequestMessage(new HttpMethod("GET"), "/");

       // Act
       var response = await Client.SendAsync(request);

       //assert etc
    }
}

In order to use MVC, and ensure your views can be discovered, you should update your WebHostBuilder to set the content path:

var path = PlatformServices.Default.Application.ApplicationBasePath;
var setDir = Path.GetFullPath(Path.Combine(path, <projectpathdirectory> ));

var builder = new WebHostBuilder()
    .UseContentRoot(setDir)
    .UseStartup<TStartup>();
1
  • Thank you, I've read your blog post before posting my question actually, and from my tests it exhibits the same behaviour, I will make a repo soon with to prove this.
    – Calin
    Jun 22, 2016 at 7:24
0

What .net core version are you using? preview1?

Update to RTM (preview2--31...) Maybe you ran into a bug that is already been fixed.

2
  • .net core rc2 it's in the title of the question, the full version has been released I will have test with that.
    – Calin
    Jul 1, 2016 at 4:30
  • sorry, for me RC2 is preview1 :D as the SDK
    – Eric Liu
    Jul 1, 2016 at 5:10
0

You cannot override your services by test services that way. What I did was create methods which added dependencies and made the method overidable. Then in my test I would override that method and inject different dependencies.

I explain this also in my blog: http://www.stefanhendriks.com/2016/04/29/integration-testing-your-dot-net-core-app-with-an-in-memory-database/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.