I am new to unit testing and it sounds to me like it should be easy to get NSubstitute to be able to return null for a method but I cannot get it to work.
I have tried this for a Get method that should return a Campaign method
_campaigns = Substitute.For<IOptions<Campaigns>>();
_campaigns.Get(Arg.Any<string>()).Returns(null);
In production I use a FirstOrDefault() method to return the campaign object and it returns null if it does not exist. So in my unit test I want to test that case, but I cannot fake it with NSubstitute as I get the following error when compiling:
error CS0121: The call is ambiguous between the following methods or properties: 'SubstituteExtensions.Returns(T, T, params T[])' and 'SubstituteExtensions.Returns(T, Func, params Func[])'
I do this to avoid the error:
_campaigns.Get(Arg.Any<string>()).Returns((Campaign)null);
but then I get an execution error on that line:
System.NullReferenceException : Object reference not set to an instance of an object.
In nowdays NSubsitute has method it calls ReturnsNull
or expression .Returns(l => null)
https://github.com/nsubstitute/NSubstitute/pull/181
I think that this topic should be closed by moderator