2012-04-10 64 views
1

我在C#中編寫服務器組件,並使用Pex進行單元測試。Pex在探索時引發NullReferenceException

我有一個複雜的參數化單元測試的具體方法。現在事實證明,只要我添加了某個斷言塊,一些pex探索運行就會失敗,在我的方法的最後一行發生NullReferenceException(右括號)。當我調試失敗的運行時,它運行得非常好。

我犯了一個錯誤還是這是PEX中的錯誤?

謝謝!

[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遇到一些問題。我檢查了每一次運行,甚至僞造了主機的啓動方法。沒有異步方法。我確實創造了在某些情況下1級的任務,但我運行它同步的IHostEndpoint存根使用TaskCompletionSource用值/狀態直接設置創建(以下證明)

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

+0

什麼是堆棧跟蹤? – SLaks 2012-04-10 16:00:30

+0

System.NullReferenceException:未將對象引用設置爲對象的實例。 c:\ users \ koen \ documents \ visual studio 2010 \ Projects \ ManagedHttp \ ManagedHttp.Tests \ HostTest.cs(98):at System.Threading.Tasks.Task ManagedHttp.HostTest.Start(System.Threading.CancellationToken cancellationToken,System .Int32 workingEndpoints,System.Int32 failingEndpoints,System.Int32 brokenEndpoints) 98行是包含方法的右括號的行 – Polity 2012-04-10 16:01:55

+0

您發佈的代碼中的哪一行對應於>? – ChrisF 2012-04-10 16:07:48

回答

0

Pex是一個很棒的工具,但它有缺陷。從你的陳述中我可以看出這是Pex中的一個錯誤:添加斷言不應該導致無關的NullRef。我建議你在Pex論壇上報告。

相關問題