5

In owin, its possible to test a web api in unit test using a TestServer (see this blog).

Is this functionality available for asp.net 5 middleware ?

UPDATE:

based on responses below, I tried to use TestServer, but visual studio complains that 'The type or namespace name 'AspNet' does not exist in the namespace 'Microsoft' (are you .....'

  • I use Visual Studio 2015

  • in my nuget sources (settings) I have (https://www.myget.org/F/aspnetmaster/) (I also tried with https://www.myget.org/F/aspnetvnext/, but got the same problem)

  • here is my project.json file

    {
        "version": "1.0.0-*",
        "dependencies": {
            "Microsoft.AspNet.Http": "1.0.0-*",        
            "Microsoft.AspNet.TestHost":  "1.0.0-*",
            "Microsoft.AspNet.Hosting":  "1.0.0-*", 
            "Microsoft.AspNet.Testing" :  "1.0.0-*",
            "xunit": "2.1.0-beta1-*",
            "xunit.runner.aspnet": "2.1.0-beta1-*",
            "Moq": "4.2.1312.1622",
            "Shouldly": "2.4.0"
        },
        "commands": {
            "test": "xunit.runner.aspnet"
        },
        "frameworks" : {
            "aspnet50" : {
                "dependencies": {
                }
            }
        }
    }
    
Cedric Dumont
  • 1,009
  • 17
  • 38
  • What makes you wonder about this? Even if you cannot do it, you can duplicate your app and have two identical prod servers, from which you will use one as your test server. – Lajos Arpad Apr 10 '15 at 12:23
  • In fact what I would like is a inMemory test server, that runs during a unit test and then closes. – Cedric Dumont Apr 10 '15 at 12:35
  • Yes, that would be like that, except that it does not disturb your prod application. – Lajos Arpad Apr 10 '15 at 12:46
  • based on @tugberk answer, I created a [repo](https://github.com/CedricDumont/vnext-playground/tree/master/MultiTestServer) to use this feature – Cedric Dumont Apr 16 '15 at 07:17

1 Answers1

11

It is available on ASP.NET 5 as well: Microsoft.AspNet.TestHost.

Here is a sample. Middleware:

public class DummyMiddleware
{
    private readonly RequestDelegate _next;

    public DummyMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        Console.WriteLine("DummyMiddleware");
        context.Response.ContentType = "text/html";
        context.Response.StatusCode = 200;

        await context.Response.WriteAsync("hello world");
    }
}

Test:

[Fact]
public async Task Should_give_200_Response()
{
    var server = TestServer.Create((app) => 
    {
        app.UseMiddleware<DummyMiddleware>();
    });

    using(server)
    {
        var response = await server.CreateClient().GetAsync("/");
        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
    }
}

You can find more about the usage of the TestServer class on the tests.

tugberk
  • 57,477
  • 67
  • 243
  • 335
  • Thanks for the answer, could you tell me which nuget source you are using ? because I tried to import these packeges in my project.json, but I cannot reference TestServer because when using Microsoft.AspNet.TestHost; visual studio complains that namespace AspNet does not exists. I will update my question so that you can look at my project.json. – Cedric Dumont Apr 13 '15 at 08:53
  • 1
    @CedricDumont aspnetrelease feed has it: https://www.myget.org/gallery/aspnetrelease – tugberk Apr 13 '15 at 12:51
  • Hi @tugberk, in the same spirit I posted another question in this same spirit. If you have few minutes to look at, it would help me a lot : [using two testServer](http://stackoverflow.com/questions/29621267/using-asp-net-5-testserver-to-mock-an-external-api-call) – Cedric Dumont Apr 14 '15 at 07:16