如何在aspnet核心中为单元测试设置Request头

mje*_*son 7 .net c# xunit.net asp.net-core

使用:NETCore.App 1.1,xunit 2.2.0-beta4-build3444,Moq 4.6.38-alpha.

我试图测试一个ASP.Net核心控制器,它需要一个名为"X-ClientID"的标头出现在请求中.我现在写了我的测试函数如下:

[Fact]
public void Test1()
{
    var controller = new GraphController(this.positiveAuthorizer, this.loggerFactory, this.elasticsearchSettings);

    controller.Request.Headers.Add("X-ClientID", "1234");

    var result = controller.Get("calls", "year", "day", "1", "sliding", new DateTime(2016, 11, 1)).Result;

    // TODO: Write assert statements on result
}
Run Code Online (Sandbox Code Playgroud)

positiveAuthorizer,loggerFactory并且elasticsearchSettings是使用Moq的模拟类,它们在控制器内按预期工作.

不过controller.Requestnull.此外,controller.Request是get-only,所以我无法将其设置为新值.

如何在调用控制器之前设置X-Client标头?

归档时间:

查看次数:

1292 次

最近记录:

8 年,11 月 前