2017-05-21 5 views
1

私はいくつかの私のWebサービスをテストしようとしています。このために、私はHttpResponseMessageのいくつかを主張したいと思います。このために、私がやっている:失敗したとしてユニットテストHttpResponseMessage

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

しかし、テストは常にLogin()戻っOk()は、私がデバッグするときにもかかわらず、返す:Loginはメソッドのようなものです

[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"); 
} 

を。私はテストをデバッグするときに見かけ上のエラーまたは例外んが、しかし、出力は(私はちょうどLoadedまたはUnloadedだかを示すラインを除外している)のようなものを示しています

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). 

私の推測では、これが方法ではありませんということですIHttpActionResultHttpResponseMessageに変換する必要があります。この問題を解決するにはどうすればよいですか?

答えて

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"); 
} 
関連する問題