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

AmbiguousMatchException when setting up the property, that hides another one #939

Merged
merged 4 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Moq/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ internal static PropertyInfo GetReboundProperty(this MemberExpression expression
// we "upgrade" to the derived property.
if (property.DeclaringType != expression.Expression.Type)
{
var derivedProperty = expression.Expression.Type.GetProperty(property.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
var derivedProperty = expression.Expression.Type
.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.SingleOrDefault(p => p.Name == property.Name && p.PropertyType == property.PropertyType);
stakx marked this conversation as resolved.
Show resolved Hide resolved
if (derivedProperty != null && derivedProperty.GetMethod.GetBaseDefinition() == property.GetMethod)
{
return derivedProperty;
Expand Down
35 changes: 35 additions & 0 deletions tests/Moq.Tests/HidePropertyFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD.
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.

using Xunit;

namespace Moq.Tests
{
public class HidePropertyFixture
{
public class A
{
public string Prop { get; }
}

public class B : A
{
public new virtual int Prop { get; }
}

public class C : B
{
}

[Fact]
public void SetupsDerivedProperty()
{
var mock = new Mock<C>();
var value = 5;

mock.Setup(m => m.Prop).Returns(value);

Assert.Equal(value, mock.Object.Prop);
}
}
}