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

SetupAllProperties does not recognize property as read-write if only setter is overridden #886

Closed
stakx opened this issue Aug 10, 2019 · 3 comments · Fixed by #950
Closed
Assignees
Labels
Milestone

Comments

@stakx
Copy link
Contributor

stakx commented Aug 10, 2019

Probably an edge case, but it demonstrates that Moq's reflection logic regarding type members and overrides in type hierarchies still comes to wrong conclusions sometimes. It is probable that there are other places in Moq suffering from the same problem.

Steps to reproduce:

public class WithAutoProperty
{
    public virtual object Property { get; set; }
}

public class OverridesOnlySetter : WithAutoProperty
{
    public override object Property { set => base.Property = value; }
}

[Fact]
public void SetupAllProperties_retains_value_of_derived_read_write_property_that_overrides_only_setter()
{
    var mock = new Mock<OverridesOnlySetter>();
    mock.SetupAllProperties();
    mock.Object.Property = "value";
    Assert.Equal("value", mock.Object.Property);
}

Expected outcome:

The test passes. It should be possible to set and subsequently query that property, just like in C#:

var obj = new OverridesOnlySetter();
obj.Property = "value";
Assert.Equal("value", obj.Property);

Actual outcome:

The test fails because querying mock.Object.Property will yield null instead of the value it was set to.

Additional details:

In order to find the cause for this, it's instructive to also consider the reverse case where only get getter gets overridden. (That case works fine.) The first call to a property's accessor will determine which property gets set up: WithAutoProperty's, or OverridesOnlySetter's / OverridesOnlyGetter's.

@stakx stakx added the bug label Aug 10, 2019
@stakx stakx self-assigned this Aug 10, 2019
@stakx
Copy link
Contributor Author

stakx commented Aug 10, 2019

Another failing test:

[Fact]
public void SetupGet_succeeds_for_derived_read_write_property_that_overrides_only_setter()
{
    var mock = new Mock<OverridesOnlySetter>();
    mock.SetupGet(m => m.Property);
}
System.NullReferenceException: Object reference not set to an instance of an object.
   at Moq.ExpressionExtensions.GetReboundProperty(MemberExpression expression) in C:\Users\stakx\Projects\moq4\src\Moq\ExpressionExtensions.cs:line 312
   at Moq.ExpressionExtensions.ToPropertyInfo(LambdaExpression expression) in C:\Users\stakx\Projects\moq4\src\Moq\ExpressionExtensions.cs:line 328
   at Moq.Mock.SetupGet(Mock mock, LambdaExpression expression, Condition condition) in C:\Users\stakx\Projects\moq4\src\Moq\Mock.cs:line 496
   at Moq.Mock`1.SetupGet[TProperty](Expression`1 expression) in C:\Users\stakx\Projects\moq4\src\Moq\Mock.Generic.cs:line 347

@stakx stakx added this to the 4.13.1 milestone Aug 11, 2019
@ishimko
Copy link
Contributor

ishimko commented Sep 3, 2019

I've tried to get into this problem just out of curiosity and ended up asking questions in SO about reflection behavior for virtual properties.

Maybe it can be helpful:
https://stackoverflow.com/questions/57762322
https://stackoverflow.com/questions/57775951

(at the moment of posting this comment the second one does not have any answers)

@stakx
Copy link
Contributor Author

stakx commented Sep 5, 2019

Also related (might become relevant in the future): dotnet/csharplang#1568

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants