无法在C#WEB API REST服务单元测试中模拟Owin上下文

时间:2016-08-12 14:02:50

标签: c# unit-testing moq owin

我试图在C#WEB API REST服务中模拟OWIN上下文,但OWIN上下文似乎总是为null。我正在使用moq和.NET framework 4.5.2。

这是我试图测试的控制器方法:

public class CustomerController : ApiController
{
    // GET: api/Customer/n
    [HttpGet]
    [ClaimsPrincipalPermission(SecurityAction.Demand, Operation = Actions.View, Resource = Resources.Self)]
    public IHttpActionResult Get(int id)
    {
        if (id <= 0)
            return BadRequest();

        ClaimsPrincipalPermission.CheckAccess(id.ToString(),"UserId");

        string jsonResponse = string.Empty;
        HttpStatusCode returnCode = this.ForwardGetRequestToWallet(String.Format("customer/gethistory/{0}", id), out jsonResponse);
        if (returnCode== HttpStatusCode.OK)
            return Ok(jsonResponse);
        return StatusCode(returnCode);            
    } 

在一个帮助器类中,出现错误的代码,在controller.Request.GetOwinContext上调用:

public static HttpClient GetClient(this ApiController controller)
{
    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Authorization = controller.Request.Headers.Authorization;
    var test = controller.Request.GetOwinContext().Request.RemoteIpAddress;
    return client;
} 

我的测试初始化​​:

[TestInitialize()]
public void MyTestInitialize() 
{
    player_user_id = Convert.ToInt32(ConfigurationManager.AppSettings["PLAYER_USER_ID"]);
    player_user_name = ConfigurationManager.AppSettings["PLAYER_USER_NAME"];
    API_protocol = ConfigurationManager.AppSettings["WEB_API_PROTOCOL"];
    API_host = ConfigurationManager.AppSettings["WEB_API_HOST"];
    API_port = ConfigurationManager.AppSettings["WEB_API_PORT"];

    wrapper = new ClaimsPrincipalWrapper();

    server = new Mock<HttpServerUtilityBase>();
    response = new Mock<HttpResponseBase>();

    request = new Mock<HttpRequestBase>();

    session = new Mock<HttpSessionStateBase>();
    session.Setup(s => s.SessionID).Returns(Guid.NewGuid().ToString());

    config = new HttpConfiguration();

    owinContext = new Mock<IOwinContext>();

    identity = new GenericIdentity(player_user_name);
    identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", player_user_id.ToString()));

    principal = new GenericPrincipal(identity, new[] { "User" });

    context = new Mock<HttpContextBase>();
    context.SetupGet(c => c.Request).Returns(request.Object);
    context.SetupGet(c => c.Response).Returns(response.Object);
    context.SetupGet(c => c.Server).Returns(server.Object);
    context.SetupGet(c => c.Session).Returns(session.Object);
    context.SetupGet(p => p.User.Identity.Name).Returns(player_user_name);
    context.SetupGet(p => p.Request.IsAuthenticated).Returns(true);
    context.SetupGet(s => s.User).Returns(principal);

}

我的测试:

[TestMethod]
public void Get()
{
    var queryMock = new Mock<IReadableStringCollection>();

    var requestMock = new Mock<IOwinRequest>();

    requestMock.Setup(m => m.Query).Returns(queryMock.Object);

    owinContext.Setup(m => m.Request).Returns(requestMock.Object);

    testURI = new Uri(String.Format("{0}://{1}:{2}/Get/{3}", API_protocol, API_host, API_port, player_user_id));
    request.Setup(r => r.Url).Returns(testURI);

    message = new HttpRequestMessage(HttpMethod.Get, testURI);
    var route = config.Routes.MapHttpRoute("Default", "api/{controller}/{id}");
    var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "customerController", "get" } });

    var testControllerContext = new HttpControllerContext(config, routeData, message);

    var controller = new SharedWalletAPI.Controllers.CustomerController()
    {
        ControllerContext = testControllerContext,
        Request = new HttpRequestMessage(HttpMethod.Get, testURI),
        User = new System.Security.Principal.GenericPrincipal(identity, new string[0])
    };

    // Act
    var _result = controller.Get(player_user_id);

    // get JSON
    //CustomerHistoryList jsonObj = JsonConvert.DeserializeObject<CustomerHistoryList>(_result);

    // Assert
    Assert.IsNotNull(_result);
}

所有看起来都很好,直到我们实际获取然后返回null的OWIN GetOwinContext调用,因此抛出旧栗子类型的错误&#34;对象引用未设置为对象的实例&#34;。< / p>

我仍然是OWIN的新手。有人可以在这里建议我吗?

1 个答案:

答案 0 :(得分:9)

首先,我看到你在哪里设置了你的模拟IOwinContext,但是你所展示的代码中没有一个实际上将你的模拟与请求绑定在一起。具体来说,没有任何设置会导致controller.Request.GetOwinContext()返回您的模拟对象。因此,由于您尚未设置该调用的期望,因此它将返回null。

测试

你遇到的真正痛苦是试图模仿别人的非常宽的界面(复数)。这总是很痛苦的事情。作为证据,请查看您为初始化和执行测试而必须编写的代码量。我会尝试隐藏对接口的访问,这些接口更集中,易于模拟/存根/虚拟。

就像我一样对单位测试狂热者,我不会孤立地测试我的WebAPI或MVC控制器。它们几乎总是依赖于我不想虚拟的外部事物,如HttpContext,身份验证问题,方法级属性。相反,我保持控制器尽可能薄,主要是限制方法将内容编组进出内容等等。所有业务逻辑都被移动到控制器所依赖的类中。然后,这些类更容易测试,因为它们不依赖于伪造的东西。

我仍然编写运行控制器方法的测试,通常它们涵盖了控制器的每个分支。只是这些是系统级功能测试,例如MVC应用程序的自动浏览器测试或使用HttpClient针对WebAPI应用程序访问API的测试。如果控制器保持很薄,那么首先就没有太多可以获得测试覆盖率。