Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NSSubstitute:收到的检查方法错误_C#_Testing_Nunit_Nsubstitute - Fatal编程技术网

C# NSSubstitute:收到的检查方法错误

C# NSSubstitute:收到的检查方法错误,c#,testing,nunit,nsubstitute,C#,Testing,Nunit,Nsubstitute,我有一个测试,NSubstitute在一个假类中检查错误调用。当我像下面的代码一样执行测试时,接收(…)方法检查值factory.featureClassName是否返回一次 [Test] public void CreateDataController_WhenCalled_CreatesServiceSettings() { var factory = Substitute.ForPartsOf<AbstractDataServiceFactoryFake>("fileN

我有一个测试,NSubstitute在一个假类中检查错误调用。当我像下面的代码一样执行测试时,
接收(…)
方法检查值
factory.featureClassName
是否返回一次

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

    factory.CreateDataController();

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

似乎,
received()
方法没有直接连接到调用后给出的方法。有人能解释一下为什么会发生这种情况吗?

这是NSubstitute语法的一个限制

让我们分析一下第二个代码示例会发生什么:

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看到的第二个代码示例如下:

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

为了避免这种情况,在断言(如
接收的
)或配置(如
返回的
)过程中避免调用替换项是很有用的。

这是NSSubstitute语法的一个限制

让我们分析一下第二个代码示例会发生什么:

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看到的第二个代码示例如下:

var _ = factory.Received(1).FeatureClassName;
factory.CreateServiceSettings("fileName", _);
为了避免这种情况,在断言(如
接收的
)或配置(如
返回的
)期间,避免调用替换项是很有用的