具有响应 headers 的单元测试 webapi 控制器

Unit test webapi controller with response headers

我正在尝试学习 webapi 并且偶然发现了一个问题。我正在做的培训课程展示了如何通过返回响应 header 和下一个和上一个 link 来进行分页。然而,它使用 HttpContext.Current.Response.Headers.Add() 发回下一个 link、上一个 link 和总页数。

我也在尝试为控制器实施单元测试。问题似乎是 HttpContext.Currentnull 当 运行 通过单元测试时。我在某处读到我不应该 HttpContext.Current 用于 webapi,因为它不可测试,但我不确定我应该使用什么。

这是我的控制器代码:

public partial class CandidateManagerController
{
    private readonly ICandidateManager _candidateManagerV2;

    public CandidateManagerController(ICandidateManager candidateManager)
    {
        _candidateManagerV2 = candidateManager;
    }

    [VersionedRoute("CandidateManager", 2, Name="CandidateManagerV2")]
    public IHttpActionResult Get(int page = 1, int pageSize = 1)
    {
        try
        {
            var totalCount = 0;
            var totalPages = 0;

            var result = _candidateManagerV2.GetCandidates(out totalCount, out totalPages, page, pageSize);

            var urlHelper = new UrlHelper(Request);


            var prevLink = page > 1
                ? urlHelper.Link("CandidateManagerV2",
                    new
                    {
                        page = page - 1,
                        pageSize = pageSize,
                    })
                : "";


            var nextLink = page < totalPages ? urlHelper.Link("CandidateManagerV2",
                new
                {
                    page = page + 1,
                    pageSize = pageSize
                }) : "";

            var paginationHeader = new
            {
                currentPage = page,
                pageSize = pageSize,
                totalCount = totalCount,
                totalPages = totalPages,
                previousPageLink = prevLink,
                nextPageLink = nextLink
            };

            HttpContext.Current.Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationHeader));



            return Ok(result);
        }
        catch (Exception exp)
        {
            return InternalServerError();
        }
    }

}

这是我的单元测试。请注意我使用的是 Nunit 和最小起订量:

[TestFixture]
public class CandidateManagerControllerV2Tests
{


    [Test]
    [Category("CandidateManagerController Unit Tests")]
    public void Should_Return_List_Of_Candidate_Objects()
    {

        var testList = new List<Candidate>();
        testList.Add(new Candidate() { CandidateId = 1, Name = "Mr", Surname = "Flibble" });
        testList.Add(new Candidate() { CandidateId = 2, Name = "Arnold", Surname = "Rimmer" });

        var totalCount = 0;
        var totalPages = 0;
        var mockManager = new Mock<ICandidateManager>();
        mockManager.Setup(x => x.GetCandidates(out totalCount, out totalPages, It.IsAny<int>(), It.IsAny<int>())).Returns(testList);

        var controller = new CandidateManagerController(mockManager.Object);
        SetupControllerForTests(controller);

        var result = controller.Get(1, 1);
    }

    private static void SetupControllerForTests(ApiController controller)
    {
        var config = new HttpConfiguration();
        var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/candidatemanager");
        var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
        var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "products" } });

        controller.ControllerContext = new HttpControllerContext(config, routeData, request);
        controller.Request = request;
        controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
        controller.ActionContext=new HttpActionContext();
    }

}

我希望有人能够帮助我。可能是我在实现分页的方式上走错了路。但是,我可能需要以任何方式添加响应 header。

您应该避免将自己耦合到 HttpContext

这是另一种设置 header 并且仍然能够按预期对其进行单元测试的方法。您创建一个 HttpResponseMessage,根据需要添加 header,然后从中创建一个 ResponseMessageResult

//...code removed for brevity

var response = Request.CreateResponse(HttpStatusCode.OK, result);

response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationHeader));

IHttpActionResult ok = ResponseMessage(response);

return ok;

您还应注意,您的控制器设置会在创建 UrlHelper 时导致空引用错误,因为您在分配默认 ActionContext[=19= 时将控制器的 Request 重置为空]

private static void SetupControllerForTests(ApiController controller) {
    var config = new HttpConfiguration();
    var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/candidatemanager");
    var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
    var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "products" } });

    controller.ControllerContext = new HttpControllerContext(config, routeData, request);
    controller.Request = request;
    controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
    //commented this out as it was causing Request to be null
    //controller.ActionContext=new HttpActionContext();
}

检查 X-Pagination header

时通过了以下测试
public async Task Should_Return_Paged_List_Of_Candidate_Objects() {
    //Arrange
    var testList = new List<Candidate>();
    testList.Add(new Candidate() { CandidateId = 1, Name = "Mr", Surname = "Flibble" });
    testList.Add(new Candidate() { CandidateId = 2, Name = "Arnold", Surname = "Rimmer" });

    var totalCount = 0;
    var totalPages = 0;
    var mockManager = new Mock<ICandidateManager>();
    mockManager.Setup(x => x.GetCandidates(out totalCount, out totalPages, It.IsAny<int>(), It.IsAny<int>())).Returns(testList);

    var controller = new CandidateManagerController(mockManager.Object);
    SetupControllerForTests(controller);

    //Act
    var response = await controller.Get(1, 1).ExecuteAsync(System.Threading.CancellationToken.None);

    //Assert
    Assert.IsNotNull(response);
    Assert.IsInstanceOfType(response, typeof(HttpResponseMessage));
    Assert.IsTrue(response.Headers.Contains("X-Pagination"));
}