从测试服务器测试asp.net 5 vnext中间件。

时间:2021-06-07 02:42:43

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

在owin中,可以使用TestServer在单元测试中测试web api(参见本博客)。

Is this functionality available for asp.net 5 middleware ?

这个功能对于asp.net 5中间件来说是可用的吗?

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 .....'

基于下面的响应,我尝试使用TestServer,但visual studio抱怨“类型或名称空间名称‘AspNet’在名称空间‘Microsoft’中不存在(您是……”

  • I use Visual Studio 2015

    我使用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)

    在我的nuget源(设置)中,我有(https://www.myget.org/F/aspnetmaster/)(我也尝试了https://www.myget.org/F/aspnetvnext/,但也遇到了同样的问题)

  • here is my project.json file

    这是我的项目。json文件

    {
        "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": {
                }
            }
        }
    }
    

1 个解决方案

#1


11  

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

它可以在ASP中使用。NET 5: 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.

您可以在测试中找到更多关于TestServer类的用法。

#1


11  

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

它可以在ASP中使用。NET 5: 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.

您可以在测试中找到更多关于TestServer类的用法。