2016-07-22 3 views
0

NSubstituteが間違った呼び出しを偽のクラスでチェックするテストがあります。次のコードのようなテストを行うと、Received(...)メソッドは、値factory.featureClassNameが1回返されることを確認します。Nsubstitute:受信したメソッドが間違っているかどうかを確認します

[Test] 
public void CreateDataController_WhenCalled_CreatesServiceSettings() 
{ 
    var factory = Substitute.ForPartsOf<AbstractDataServiceFactoryFake>("fileName"); 
    var featureClassName = factory.FeatureClassName; 

    factory.CreateDataController(); 

    factory.Received(1).CreateServiceSettings("fileName", featureClassName); 
} 

に思えRecieved()メソッドを直接指定したメソッドに接続されていないことを、私は次のコードを使用する必要が一度

[Test] 
public void CreateDataController_WhenCalled_CreatesServiceSettings() 
{ 
    var factory = Substitute.ForPartsOf<AbstractDataServiceFactoryFake>("fileName"); 

    factory.CreateDataController(); 

    factory.Received(1).CreateServiceSettings("fileName", factory.FeatureClassName); 
} 

はメソッド CreateServiceSettings(...)が呼び出されることを(意図したように)テストするにはコール後。誰が私を説明することができます、なぜこれが起こっている?

答えて

0

これはNS substituteの構文の制限です。

のは、2番目のコードサンプルで何が起こるかを打破してみましょう:最初のコードサンプルで

factory 
    .Received(1) // ... check the next call has previously been received 
    .CreateServiceSettings("fileName", className) 
        // call is made to CreateServiceSettings, NSub checks 
        // it was received. 

、我々は、この代わりに取得する:

つまり
factory 
    .Received(1) // ... check the next call has previously been received 
    .CreateServiceSettings("fileName", factory.FeatureClassName) 
        // before CreateServiceSettings is invoked, its arguments 
        // must be evaluated. So factory.FeatureClassName 
        // is called next and NSubstitute checks that. 

は、NSubstituteは、2番目のコードサンプルを見て

var _ = factory.Received(1).FeatureClassName; 
factory.CreateServiceSettings("fileName", _); 

これを避けるには、 (Receivedのような)アサーション中または(Returnsのような)コンフィギュレーションの間のitute。

関連する問題