I was doing dome unit tests around my repository, until I got a strange error. Searched around to see if I was not making known mistakes, I could simplify it and notice that I am getting the same error. Looks like I cannot mock IFindFluent interface properly, and I would like to know what I am doing wrong. This test:
[Fact]
public void Test()
{
var ff = Substitute.For<IFindFluent<string, string>>();
ff.FirstOrDefaultAsync().Returns("asd");
}
is returning this error message:
NSubstitute.Exceptions.CouldNotSetReturnDueToTypeMismatchException : Can not return value of type Task`1 for IDisposable.Dispose (expected type Void).
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);
at NSubstitute.Core.ConfigureCall.CheckResultIsCompatibleWithCall(IReturn valueToReturn, ICallSpecification spec) at NSubstitute.Core.ConfigureCall.SetResultForLastCall(IReturn valueToReturn, MatchArgs matchArgs) at NSubstitute.Core.SubstitutionContext.LastCallShouldReturn(IReturn value, MatchArgs matchArgs) at CorporateActions.Tests.Persistence.RepositoryTests.Test()
I've searched around, but most common mistakes about it does not fit with this simple implementation. Any thoughts why does this not work?