I want to check if an anonymous function has been called with NSubstitute. A method in a class I have takes a Func<>
parameter, and I want to make sure this parameter is called (or not called). I have tried the following, but it does not seem to work:
var spy = Substitute.For<Func<string, int>>();
MyClass.DoSomething(spy);
spy.Invoke(Arg.Any<string>()).Received();
This however throws an exception:
NSubstitute.Exceptions.NullSubstituteReferenceException : NSubstitute extension methods like .Received can only be called on objects created using Substitute.For<T>() and related methods.
Try replacing
spy.Invoke(Arg.Any<string>()).Received();
with
spy.Received().Invoke(Arg.Any<string>());
There's no difference if your production code calls the func via spy.Invoke()
or via spy()
. But the test won't succeed, if your code calls BeginInvoke
. But I think that shouldn't be a problem, because you should know in advance if a test for Invoke
or BeginInvoke
is needed.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments