Skip to content
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

Setup does not recognize all virtual sealed methods as uninterceptable #496

Closed
stakx opened this issue Oct 22, 2017 · 0 comments
Closed
Assignees
Labels
Milestone

Comments

@stakx
Copy link
Contributor

stakx commented Oct 22, 2017

Moq (4.7.142) does not recognize all sealed methods as uninterceptable. Given a sealed overridden property in a derived class, it does not throw an exception when an attempt is made to Setup that property:

public abstract class FooBase
{
    public abstract object Property { get; }
}

public class Foo : FooBase
{
    public sealed override object Property => null;
}

[Fact] // does not pass
public void Cannot_setup_virtual_sealed_property()
{
    // verify that Property is both virtual and sealed:
    var propertyGetter = typeof(Foo).GetProperty("Property").GetGetMethod();
    Assert.True(propertyGetter.IsVirtual && propertyGetter.IsFinal);

    // arrange:
    var fooMock = new Mock<Foo>();

    // act:
    var exception = Record.Exception(() =>
    {
        fooMock.Setup(foo => foo.Property).Returns("mocked");
    });
    var property = fooMock.Object.Property;

    // assert:
    Assert.NotEqual("mocked", property); // Moq shouldn't be able to intercept a sealed property
    Assert.NotNull(exception);           // and warn us by throwing an exception.
}
@stakx stakx added the bug label Oct 22, 2017
@stakx stakx added this to the v4.8.0 milestone Oct 22, 2017
@stakx stakx self-assigned this Oct 22, 2017
@stakx stakx closed this as completed Oct 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant