【问题标题】:Unit testing HttpResponseMessage单元测试 HttpResponseMessage
【发布时间】:2017-05-21 18:29:06
【问题描述】:

我正在尝试对我的一些网络服务进行单元测试。为此,我想声明我的一些HttpResponseMessages。为此,我正在做:

[TestMethod()]
public void LoginTest()
{
    HttpResponseMessage response = Request.CreateResponse(_accountController.Login("testuser", "testpw")); //CODE STOPS RUNNING AT THIS LINE
    Assert.IsTrue(response.IsSuccessStatusCode, "User unable to log in with correct login info");
}

Login 是这样的方法:

public IHttpActionResult Login(String userName, String userPassword)
{
    try
    {
        if (userName == null)
            return NotFound();
        //Logging in user etc.
        return Ok();
    }
    catch
    {
        return NotFound();
    }
}

然而,测试总是返回失败,即使我调试时Login() 返回Ok()。没有明显的错误或异常,但是,当我调试测试时,输出显示如下内容(我已排除仅显示 LoadedUnloaded 的行):

The thread 0x1190 has exited with code 0 (0x0).
Exception thrown: 'System.InvalidOperationException' in System.ServiceModel.dll
Exception thrown: 'System.ArgumentNullException' in System.Web.Http.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll
Exception thrown: 'Microsoft.VisualStudio.TestPlatform.MSTestFramework.TestFailedException' in Microsoft.VisualStudio.TestPlatform.Extensions.VSTestIntegration.dll
Exception thrown: 'Microsoft.VisualStudio.TestPlatform.MSTestFramework.TestFailedException' in Microsoft.VisualStudio.TestPlatform.Extensions.VSTestIntegration.dll
The thread 0xaec has exited with code 0 (0x0).
Exception thrown: 'System.InvalidOperationException' in Microsoft.VisualStudio.TestPlatform.TestExecutor.Core.dll
Exception thrown: 'System.ServiceModel.CommunicationObjectAbortedException' in System.ServiceModel.dll
Exception thrown: 'System.ServiceModel.CommunicationObjectAbortedException' in System.ServiceModel.dll
Exception thrown: 'System.ServiceModel.CommunicationObjectAbortedException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.IO.IOException' in System.dll
Exception thrown: 'System.ServiceModel.CommunicationObjectAbortedException' in System.ServiceModel.dll
Exception thrown: 'System.InvalidOperationException' in Microsoft.VisualStudio.TestPlatform.TestExecutor.Core.dll
Exception thrown: 'System.ServiceModel.CommunicationObjectAbortedException' in System.ServiceModel.dll
The program '[9936] vstest.executionengine.x86.exe' has exited with code 0 (0x0).
The program '[4084] TanulmanyiRendszer.Admin.vshost.exe' has exited with code -1 (0xffffffff).

我的猜测是,这不是您应该将IHttpActionResult 转换为HttpResponseMessage 的方式。我该如何解决这个问题?

【问题讨论】:

    标签: c# web-services unit-testing asp.net-web-api


    【解决方案1】:

    如果使用IHttpActionResult 作为操作的返回类型,则检查返回的实际类型是否是正确的派生类型。无需检查HttpResponseMessage,因为这是框架的问题

    [TestMethod()]
    public void LoginTest() {
        var response = _accountController.Login("testuser", "testpw") as OkResult;
        Assert.IsNotNull(response, "User unable to log in with correct login info");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      相关资源
      最近更新 更多