简体   繁体   English

NSubstitute无法设置返回值(CouldNotSetReturnException)

[英]NSubstitute cannot setup return value (CouldNotSetReturnException)

I have an interface (called IRepository ) that has a method on it like this: 我有一个接口(称为IRepository ),上面有一个这样的方法:

IEnumerable<TEntity> ExecuteStoredProcedure<TEntity>(string functionName, 
                                      params Tuple<string, object>[] parameters);

I am trying to set what that method will return when it is called via my unit test. 我试图设置通过我的单元测试调用该方法时将返回的内容。 Like this: 像这样:

dataAccess = Substitute.For<IRepository>();
dataAccess.ExecuteStoredProcedure<MyCustomReturnType>(null, null)
          .ReturnsForAnyArgs(MyCustomReturnList);

When I run the test I get this exception: 运行测试时,出现以下异常:

NSubstitute.Exceptions.CouldNotSetReturnException: Could not find a call to return from. NSubstitute.Exceptions.CouldNotSetReturnException:找不到从其返回的调用。

The message goes on to caution about trying to do this with actual classes, but that does not apply to me. 该消息继续提醒您尝试对实际的类执行此操作,但这不适用于我。

I tried changing my null params to be something more real: 我试图将我的null参数更改为更真实的东西:

ExecuteStoredProcedure<MyCustomReturnType>("", new Tuple<string, object>[]{null})

But that did not help... 但这没有帮助...

Any ideas what I am doing wrong with this substitute? 有什么想法我用这种替代品做错了吗?

(My guess is that it has something to do with the params keyword.) (我的猜测是它与params关键字有关。)

So, this is where simplifying for Stack Overflow can get you in trouble. 因此,在这里简化堆栈溢出可能会给您带来麻烦。

The list MyCustomReturnList was actually a list held in a static class. 列表MyCustomReturnList实际上是静态类中包含的列表。 Since passing the list as the return value did not actual affect the class, the static constructor was not called (which sets up the list). 由于将列表作为返回值传递并不会实际影响类,因此未调用静态构造函数(用于设置列表)。

Somehow (not quite sure of the details), having a "reference" to a list that had not been setup yet made is so that NSubstitute could not setup the return value (probably because it was not initialized). 以某种方式(不太确定细节),对尚未设置的列表进行“引用”是为了使NSubstitute无法设置返回值(可能是因为它未初始化)。

The only thing NSubstitute could have done better was a different error message. NSubstitute可以做得更好的唯一事情是一条不同的错误消息。 But the error was in my code, not with NSubstitute. 但是错误出在我的代码中,而不是NSubstitute。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM