Wednesday, May 6, 2015

getting back null from .Returns in NSubstitute

If this is normal mocking...

Repo.GetLastName(Arg.Any<string>()).Returns("Smith");

 
 

...why won't this work?

Repo.GetLastName(Arg.Any<string>()).Returns(null);

 
 

For some reason NSubstitute, in expecting to return a string in this example, cannot associate a null string with the string type. It's authors seem happy to let us do some of the work ourselves and thus we must undertake a wire-up like so to hack around the inability to use one line of intuitive code.

string nullString = null;
Repo.GetLastName(Arg.Any<string>()).Returns(nullString);

 
 

If you need an interface to come back null you have to do it the same way too.

No comments:

Post a Comment