moq icon indicating copy to clipboard operation
moq copied to clipboard

Mocking an interface that has a sealed implementation of a method

Open ricardo-noyolalozano opened this issue 2 years ago • 3 comments

public interface MyInterface
{
    public sealed Task SealedMethod(Foo foo) => UnsealedMethod(foo.Name);
}

When I mock this interface and inject into the class I am testing, I get an exception thrown about it missing the sealed method. I was wondering whether there would be any support for this kind of interface.

Back this issue Back this issue

ricardo-noyolalozano avatar Mar 01 '23 17:03 ricardo-noyolalozano

This has to happen over at DynamicProxy's repo, see their issue at https://github.com/castleproject/Core/issues/447.

stakx avatar Mar 01 '23 19:03 stakx

Due to lack of recent activity, this issue has been labeled as 'stale'. It will be closed if no further activity occurs within 30 more days. Any new comment will remove the label.

github-actions[bot] avatar Aug 24 '24 20:08 github-actions[bot]

Please add support for interface default implementation, not just sealed members. For example:

public interface IInterface
{
    int Value { get; }
    string FormattedValue => Value.ToString();
}

var mock = new Mock<IInterface>();
mock.SetupGet(inter => inter.Value).Returns(5);
IInterface interObj = mock.Object;

var result = interObj.FormattedValue; // Expected "5", Actual null

FormattedValue should also work for MockBehavior.Strict.

weitzhandler avatar Apr 01 '25 07:04 weitzhandler