-
Notifications
You must be signed in to change notification settings - Fork 268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Testing default interface methods in a mocked interface #703
Comments
Note that Moq supports this since April 2022 (see https://jeremybytes.blogspot.com/2019/09/c-8-interfaces-unit-testing-default.html). It's a pity NSubstitute does not support it. |
Hi @ursmeili , |
@dtchepak sorry, no, I have no spare time currently. |
This is still an issue, right? If so, is there some starting tips for a PR to handle this, @dtchepak ? I can try to contribute, but I have no idea about where to start... |
Hi @andreminelli , Here's a test to start: #if NET5_0_OR_GREATER
using System.Linq;
using NUnit.Framework;
namespace NSubstitute.Acceptance.Specs.FieldReports
{
public class InterfaceWithDefaults
{
// From https://github.com/nsubstitute/NSubstitute/issues/703
public interface IHaveDefaultMembers
{
int[] Numbers { get; }
bool HasNumber(int n) => this.Numbers.Contains(n);
}
[Test]
public void Test_Defaults()
{
var sub = Substitute.For<IHaveDefaultMembers>();
sub.Numbers.Returns(new[] { 1, 2 });
//sub.When(x => x.HasNumber(Arg.Any<int>())).CallBase();
Assert.That(sub.HasNumber(2), Is.True);
}
}
}
#endif With the
This is thrown when Hope this helps! Thanks so much for taking a look at this. 🙇 |
@dtchepak, very nice! This is much more than I was expecting 😊 But only thank me if - or "when", let's get optimistic - I could come up with some solution. |
@dtchepak , as a first look, even after solving that state problem we will probably have an issue with The approach made on Moq uses Meanwhile I will study more the NSubstitute repo and check that state problem. |
I would like to mock an interface which features a default interface method (introduced in C#8).
However, it seems like the default interface method also gets mocked away, so in the example below, the method
HasNumber()
is never called, and the test fails.Is there any solution for it already? If not, I'd request a feature to make this work.
The text was updated successfully, but these errors were encountered: