2012-04-10 3 views
1

저는 C#에서 서버 구성 요소를 작성하고 unit test를 위해 Pex를 사용합니다.Pex가 탐색 할 때 NullReferenceException을 발생시킵니다.

특정 메소드에 대해 복잡한 매개 변수가있는 단위 테스트를했습니다. 이제는 특정 assert 블록을 추가하자마자 일부 pex 탐색이 내 메서드의 닫는 줄 (대괄호 오른쪽)에서 NullReferenceException으로 실패한 것으로 나타납니다. 내가 실패한 실행을 디버깅 할 때 절대적으로 잘 돌아 간다.

내가 실수를했거나 펙스의 버그입니까?

감사합니다.

[PexMethod] 
public Task Start(CancellationToken cancellationToken, 
    int workingEndpoints, // endpoints that run succesfully 
    int failingEndpoints, // endpoints that fail immidiatly 
    int brokenEndpoints) // endpoints that return null for their task 
{ 
    PexAssume.IsTrue(workingEndpoints >= 0); 
    PexAssume.IsTrue(failingEndpoints >= 0); 
    PexAssume.IsTrue(brokenEndpoints >= 0); 
    PexAssume.IsTrue(workingEndpoints + failingEndpoints + brokenEndpoints >= 1); 

    // create fake endpoints based on the count 
    List<IHostEndpoint> fakeEndpoints = new List<IHostEndpoint>(); 
    Exception failedTaskException = new Exception(); 
    // Create a few endpoint stubs for testing purposes and add them to the list (commented away for relevance) 

    // create and start the host 
    Host host = new Host(fakeEndpoints.ToArray()); 
    Task result = host.Start(cancellationToken); 

    PexAssert.IsNotNull(result); 
    if (failingEndpoints > 0 || brokenEndpoints > 0) 
    { 
     PexAssert.IsNotNull(result.Exception); 

     int failedEndpointExceptionCount = 0; 
     int brokenEndpointExceptionCount = 0; 

     result.Exception.Flatten().Handle(innerException => 
     { 
      if (innerException == failedTaskException) 
       failedEndpointExceptionCount++; 
      else 
       brokenEndpointExceptionCount++; 

      return true; 
     }); 

     // after one broken endpoint, the run method should stop starting more endpoints 
     int brokenEndpointExpectedCount = Math.Min(1, brokenEndpoints); 
     PexAssert.AreEqual(failedEndpointExceptionCount, failingEndpoints); 
     PexAssert.AreEqual(brokenEndpointExceptionCount, brokenEndpointExpectedCount); 
    } 

    return result;    
} 

편집

한 가정은 비동기 코드로 인해, PEX는 몇 가지 문제가 발생하는 것을 수 있습니다. 모든 단일 실행을 점검하고 심지어 호스트의 시작 메소드를 가짜로 만들었습니다. 비동기 메서드는 없습니다. 나는 어떤 경우에는 한 작업을 생성 할하지만 난 기적 (아래 증거)

Task endpointTask = endpoint.Start(innerCancellationToken);     

if (endpointTask == null) 
{ 
    // This endpoint is broken, for simplicity we raise an exception in the normal pipe 
    Task faultedTask = new Task(() => 
    { 
     throw new InvalidOperationException("Endpoint returned a null valued task which is not allowed"); 
    }); 

    faultedTask.RunSynchronously(); 
    innerTasks.Add(faultedTask); 

    break; 
} 
else 
{ 
    innerTasks.Add(endpointTask); 
} 

IHostEndpoint 스텁 직접 설정 값/상태와 TaskCompletionSource를 사용하여 생성을 실행합니다.

+0

스택 추적이란 무엇입니까? – SLaks

+0

System.NullReferenceException : 개체 참조가 개체의 인스턴스로 설정되지 않았습니다. c : \ users \ koen \ documents \ visual 스튜디오 2010 \ Projects \ ManagedHttp \ ManagedHttp.Tests \ HostTest.cs (98) : System.Threading.Tasks.Task ManagedHttp.HostTest.Start (System.Threading.CancellationToken cancellationToken, System .Int32 workingEndpoints, System.Int32 failedingEndpoints, System.Int32 brokenEndpoints) 줄 98은 메서드의 닫는 괄호가 들어있는 줄입니다. – Polity

+0

게시 한 코드의 어느 줄이>에 해당합니까? – ChrisF

답변

0

Pex는 환상적인 도구이지만 버그가 있습니다. 귀하의 진술에서 나는 이것이 Pex의 버그라는 것을 알 수 있습니다. 어설 션을 추가하면 관련없는 NullRef가 발생해서는 안됩니다. Pex 포럼에서이 사실을 알려 주시기 바랍니다.

관련 문제