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

Conversation

ishatalkin
Copy link
Contributor

mock.Setup(m => m.Prop) throws AmbiguousMatchException in case that:

  • Prop hides the property with new keyword;
  • the hidden and the derived properties have different types;
  • mocked object has the child class of the class, where the property was hidden.

You can repropduce the exception with the following code:

public class HidePropertyFixture
{
	public class A
	{
		public string Prop { get; }
	}

	public class B : A
	{
		// consider new keyword and the different type of property
		public new virtual int Prop { get; }
	}

	public class C : B
	{

	}

	[Fact]
	public void SetupsDerivedProperty()
	{
		// create the mock of C; mock of B works correct
		var mock = new Mock<C>();
		// the next line will throw AmbiguousMatchException 
		mock.Setup(m => m.Prop);
	}
}

Stacktrace:

System.Reflection.AmbiguousMatchException
   в System.RuntimeType.GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
   в System.Type.GetProperty(String name, BindingFlags bindingAttr)
   в Moq.ExpressionExtensions.GetReboundProperty(MemberExpression expression) в C:\Projects\github_moq\src\Moq\ExpressionExtensions.cs:строка 323
   в Moq.ExpressionExtensions.<Split>g__Split|4_1(Expression e, Expression& r, InvocationShape& p) в C:\Projects\github_moq\src\Moq\ExpressionExtensions.cs:строка 286
   в Moq.ExpressionExtensions.Split(LambdaExpression expression) в C:\Projects\github_moq\src\Moq\ExpressionExtensions.cs:строка 107
   в Moq.Mock.SetupRecursive[TSetup](Mock mock, LambdaExpression expression, Func`3 setupLast) в C:\Projects\github_moq\src\Moq\Mock.cs:строка 590
   в Moq.Mock.Setup(Mock mock, LambdaExpression expression, Condition condition) в C:\Projects\github_moq\src\Moq\Mock.cs:строка 527
   в Moq.Mock`1.Setup[TResult](Expression`1 expression) в C:\Projects\github_moq\src\Moq\Mock.Generic.cs:строка 407

Here is pull request that fixes the bug.

This bug is somewhat similar with #930, but here is the problem of Moq.

Commit time branch was AmbiguousMatchException.
Commit time branch was AmbiguousMatchException.
Copy link
Contributor

@stakx stakx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the bug fix... Looks good to me! Apart from the request below, could you please also add an entry to the CHANGELOG.md?

 The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).


+## Unreleased
+
+#### Fixed
+
+* <description of your bug fix> (@ishatalkin, #939)
+
+
 ## 4.13.0 (2019-08-31)

Thanks!

src/Moq/ExpressionExtensions.cs Outdated Show resolved Hide resolved
@stakx stakx added the bug label Oct 16, 2019
@stakx stakx added this to the 4.13.1 milestone Oct 16, 2019
@ishatalkin
Copy link
Contributor Author

ishatalkin commented Oct 16, 2019 via email

Commit time branch was AmbiguousMatchException.

Commit time branch was AmbiguousMatchException.
Commit time branch was AmbiguousMatchException.
@ishatalkin ishatalkin requested a review from stakx October 17, 2019 08:00
@ishatalkin
Copy link
Contributor Author

@stakx, I've made the changes. Is it OK now?

Copy link
Contributor

@stakx stakx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Btw. it appears that you are using a different email address in your commits than on GitHub, so your commits won't be linked to your profile when viewed on GitHub. If you want to fix that, now would be a good time.

Otherwise I'll merge your PR tonight (in approx. 8 hours).

@ishatalkin
Copy link
Contributor Author

Btw. it appears that you are using a different email address in your commits than on GitHub, so your commits won't be linked to your profile when viewed on GitHub. If you want to fix that, now would be a good time.

Added secondary e-mail address to my profile. Feel free to merge the PR

@stakx stakx merged commit 247cb31 into devlooped:master Oct 17, 2019
@stakx
Copy link
Contributor

stakx commented Oct 17, 2019

🚀

Thank you for contributing, @ishatalkin!

@ishatalkin ishatalkin deleted the AmbiguousMatchException branch October 23, 2019 07:21
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 this pull request may close these issues.

None yet

2 participants