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

Improve Setup recognition logic for sealed methods #497

Merged
merged 3 commits into from Oct 22, 2017

Conversation

stakx
Copy link
Contributor

@stakx stakx commented Oct 22, 2017

This fixes #496.

Given the two types FooBase and Foo in that issue and the following expression tree:

Expression<Func<Foo, object>> expression = foo => foo.Property;

If one inspects the PropertyInfo that the compiler puts into expression for foo.Property, it'll be the PropertyInfo representing FooBase.Property instead of Foo.Property. (See also the this Stack Overflow question, "ReflectedType from MemberExpression".)

This PR adds code to Moq's expression.ToPropertyInfo() extension method to counteract this peculiar C# compiler behaviour.

`Setup` is expected to throw an exception if an attempt is made to
set up a property that cannot be mocked. The 4 tests added in this
commit demonstrate that Moq fails to recognize virtual, but sealed
properties as uninterceptable.
It turns out that the C# compiler puts the wrong `PropertyInfo` in an
`Expression` for properties in derived classes. Apparently, it always
puts in the `PropertyInfo` corresponding to the property's base dec-
laration instead of the `PropertyInfo` of the accessed type.

This commit adds a clause to the `expression.ToPropertyInfo` extension
method to counteract this peculiar compiler behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant