2016-11-28 3 views
0

내 응용 프로그램에 대한 테스트 작성. WebExeption - 예외를 잡을 것입니다 떨어지게에서 작업하면 마지막 버전연결 단위 테스트/C#

 [Test] 
     public void TestCreateConnection() 
     { 
      Connection testConnection = new Connection(); 
      connection.CreateConnection(correctURL, IDName + connection.ApiKey, connection.ContentType, connection.MediaType, connection.Get, false, "name"); 
      testConnection.CreateConnection(correctURL, IDName + connection.ApiKey, connection.ContentType, connection.MediaType, connection.Get, false, "name"); 
     } 

: 작동처럼 보이는 지금은 만든 방법에 대해 exeption 처리와 연결을 테스트하고 싶습니다. 이미 연결을 만들려는 내 메서드 안에 try/catch 블록에 이미 있습니다.이 메서드는 작동합니다. 하지만 내 시험에서도 필요합니다. 우리가 볼 수있는 것처럼 내가 URL 주소입니다 방법의 첫 번째 인수를 변경

[Test] 
     [ExpectedException(typeof(WebException))] 
     public void TestCreateConnection() 
     { 
      Connection testConnection = new Connection(); 
      connection.CreateConnection(correctURL, IDName + connection.ApiKey, connection.ContentType, connection.MediaType, connection.Get, false, "name"); 

      testCconnection.CreateConnection(correctURL, IDName + connection.ApiKey, connection.ContentType, connection.MediaType, connection.Get, false, "name"); 
      Assert.Catch<WebException>(() => connection.CreateConnection("test", IDName + connection.ApiKey, connection.ContentType, connection.MediaType, connection.Get, false, "name");); 
     } 

, 그것은 웹 exeption를 나오긴합니다 : 나는 그것을해야 생각하는 것 같습니다. 올바른 방법으로 어떻게 쓸 수 있습니까?

+0

제 생각에는 ExpectedExceptionAttribute 대신 Assert.Throws를 사용해야합니다. Assert.Throws의 사용은 예외를 예상하는 곳에서 더 명시 적으로 만듭니다. 귀하의 코드는 다음과 같아야합니다 : Assert.Throws (> = connection.CreateConnection (...)'.) 또한 NUnit 3.0은 공식적으로 ExpectedExceptionAttribute를 지원하지 않습니다. - 하나는 유효한 연결이고 다른 하나는 잘못된 연결입니다. –

답변

0

예외를 테스트하는 방식에 문제가 있다고 생각하지 않습니다. 그러나 테스트를 두 개의 테스트로 나누는 것이 좋습니다. 하나는 유효한 연결을 얻는 경우이고 다른 하나는 잘못된 연결을 얻는 경우입니다.

[Test] 
    public void WhenCorrectUrlIsPassedConnectionCreatedSuccessfully() 
    { 
     Connection testConnection = new Connection(); 
     connection.CreateConnection(correctURL, IDName + connection.ApiKey, connection.ContentType, connection.MediaType, connection.Get, false, "name"); 
    } 

    [Test] 
    [ExpectedException(typeof(WebException))] 
    public void WhenIncorrectUrlIsPassedThenWebExceptionIsThrown() 
    { 
     Connection connection = new Connection(); 
     Assert.Catch<WebException>(() => connection.CreateConnection("test", IDName + connection.ApiKey, connection.ContentType, connection.MediaType, connection.Get, false, "name");); 
    } 

테스트 연결을 어떻게 구현했는지에 대한 자세한 내용은 알지 못합니다. 연결을 만드는 내부 구성 요소가 있고 코드가 그 주위에 래퍼 인 경우 내부 구성 요소를 인터페이스로 전달하고 동작을 조롱하는 것을 고려해야합니다.

관련 문제