8000 Improve `Setup` recognition logic for sealed methods by stakx · Pull Request #497 · devlooped/moq · GitHub
[go: up one dir, main page]

Skip to content

Improve Setup recognition logic for sealed methods - #497

Merged
stakx merged 3 commits into
devlooped:developfrom
stakx:setup-sealed-methods
Oct 22, 2017
Merged

stakx merged 3 commits into
devlooped:developfrom
stakx:setup-sealed-methods

Conversation

@stakx
@stakx stakx commented Oct 22, 2017
Copy link
Copy Markdown
Contributor

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.

stakx added 3 commits October 22, 2017 18:24
`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.
@stakx
stakx merged commit cfad4fe into devlooped:develop Oct 22, 2017
@stakx
stakx deleted the setup-sealed-methods branch October 22, 2017 18:23
@devlooped devlooped locked and limited conversation to collaborators Sep 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0