0

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

Vaccano
  • 78,325
  • 149
  • 468
  • 850
  • This example works with the latest NSub release: https://gist.github.com/dtchepak/05179a90ad59623b21b3 Can you post any differences between that test and your code? – David Tchepak May 31 '13 at 00:54
  • Thanks for the working code. Made me realize it was my static class (that held the list) not being setup before the call. (See my answer for more details.) – Vaccano May 31 '13 at 16:58

1 Answers1

1

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.

Vaccano
  • 78,325
  • 149
  • 468
  • 850
  • 1
    Could you please send through a repro case for me? Then I can see if there is a way to detect it and improve the error message. – David Tchepak Jun 03 '13 at 03:04
  • @DavidTchepak - I tried for about an hour to get this to repro in a contained scenario. I don't know what I did, but I can't make it happen outside my WPF application... If I get time, I will try to come back to it for another attempt at reproducing it. – Vaccano Jun 03 '13 at 17:02