首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单元测试HttpResponseMessage

单元测试HttpResponseMessage
EN

Stack Overflow用户
提问于 2017-05-21 09:13:07
回答 1查看 2K关注 0票数 1

我正在尝试对我的一些web服务进行单元测试。为此,我想断言我的一些HttpResponseMessage。为此,我正在做:

代码语言:javascript
复制
[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是一种方法,如:

代码语言:javascript
复制
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的行):

代码语言:javascript
复制
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的方式。我怎样才能解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-21 09:48:28

如果使用IHttpActionResult作为操作的返回类型,那么检查返回的实际类型是否是正确的派生类型。不需要检查HttpResponseMessage,因为这是框架所关心的

代码语言:javascript
复制
[TestMethod()]
public void LoginTest() {
    var response = _accountController.Login("testuser", "testpw") as OkResult;
    Assert.IsNotNull(response, "User unable to log in with correct login info");
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44095109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档