简体   繁体   中英

NSubstitute cannot setup return value (CouldNotSetReturnException)

I have an interface (called IRepository ) that has a method on it like this:

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.

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:

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.)

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. 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).

The only thing NSubstitute could have done better was a different error message. But the error was in my code, not with NSubstitute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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