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

mock.SetupProperty throws NullReferenceException when called for partially overridden property #951

Closed
stakx opened this issue Oct 19, 2019 · 0 comments · Fixed by #952
Closed
Assignees
Labels
Milestone

Comments

@stakx
Copy link
Contributor

stakx commented Oct 19, 2019

Noticed while working on a bug fix for #886 that SetupProperty will also fail for partially overridden properties, albeit for different reasons.

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.SetupProperty(m => m.Property);
    mock.Object.Property = "value";
    Assert.Equal("value", mock.Object.Property);
}

Expected outcome:

The test passes.

Actual outcome:

System.NullReferenceException : Object reference not set to an instance of an object.
   at Moq.ExpressionExtensions.GetReboundProperty(MemberExpression expression) in src\Moq\ExpressionExtensions.cs:line 327
   at Moq.ExpressionExtensions.ToPropertyInfo(LambdaExpression expression) in src\Moq\ExpressionExtensions.cs:line 343
   at Moq.Mock`1.SetupProperty[TProperty](Expression`1 property, TProperty initialValue) in src\Moq\Mock.Generic.cs:line 659
   at Moq.Mock`1.SetupProperty[TProperty](Expression`1 property) in src\Moq\Mock.Generic.cs:line 621
   at ...
@stakx stakx added the bug label Oct 19, 2019
@stakx stakx added this to the 4.13.1 milestone Oct 19, 2019
@stakx stakx self-assigned this Oct 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant