2012-04-10 5 views
1

私はC#でサーバーコンポーネントを作成しており、unittestingにはPexを使用しています。Pexは探索中にNullReferenceExceptionを発生させます

私は、特定のメソッドに対して複雑なパラメータ化されたユニットテストを行っています。今度は、特定のアサーションブロックを追加するとすぐに、私のメソッドの終わりの行(ブラケットの右側)にあるNullReferenceExceptionで失敗したようないくつかのPEX探査が実行されます。私は失敗した実行をデバッグするとき、それは絶対にうまく動作します。

私は間違いを犯しましたか、これは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;    
} 

EDIT

一つの仮定は、非同期コードのために、Pexのは、いくつかの問題に遭遇したことがあります。私はすべての単一の実行をチェックし、さらにはホストの起動方法を偽った。非同期メソッドはありません。 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); 
} 

私はいくつかのケースでは1つのタスクを作成しませんが、私は同期的にそれを実行します(以下の証明)。

+0

スタックトレースとは何ですか? – SLaks

+0

System.NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていません。 c:\ users \ koen \ documents \ visual studio 2010 \ Projects \ ManagedHttp \ ManagedHttp.Tests \ HostTest.cs(98):System.Threading.Tasks.Task ManagedHttp.HostTest.Start(System.Threading.CancellationToken cancelationToken、System .Int32 workingEndpoints、System.Int32 failedingEndpoints、System.Int32 brokenEndpoints) 行98は、メソッドの閉じ括弧を含む行です。 – Polity

+0

投稿したコードのどの行が>に対応していますか? – ChrisF

答えて

0

Pexは素晴らしいツールですが、バグがあります。あなたのステートメントから、これはPexのバグであることがわかります。アサーションを追加すると無関係なNullRefが発生してはなりません。私はPexフォーラムでこれを報告することをお勧めします。

関連する問題