I'm using the latest version of NSubstitute, and I get the following error:
NSubstitute.Exceptions.CouldNotSetReturnDueToNoLastCallException was unhandled
HResult=-2146233088
Message=Could not find a call to return from.
Make sure you called Returns() after calling your substitute (for example: mySub.SomeMethod().Returns(value)), and that you are not configuring other substitutes within Returns() (for example, avoid this: mySub.SomeMethod().Returns(ConfigOtherSub())).
If you substituted for a class rather than an interface, check that the call to your substitute was on a virtual/abstract member. Return values cannot be configured for non-virtual/non-abstract members.
Correct use: mySub.SomeMethod().Returns(returnValue);
Potentially problematic use:
mySub.SomeMethod().Returns(ConfigOtherSub());
Instead try:
var returnValue = ConfigOtherSub();
mySub.SomeMethod().Returns(returnValue);
Here's a minimum project that replicates the error:
using System;
using NSubstitute;
public interface A
{
string GetText();
}
public class Program
{
public static void Main(string[] args)
{
var aMock = Substitute.For<A, IEquatable<string>>();
aMock.Equals("foo").Returns(true);
}
}
I suspect it's because NSubstitute cannot mock methods for which there's already an implementation, and even though these are interfaces being mocked, it's possible that the default implementation of .Equals
from object
is causing difficulties.
Is there some other way to set a return value for .Equals
in NSubstitute?