8000 AmbiguousMatchException when setting up the property, that hides another one by ishatalkin · Pull Request #939 · devlooped/moq · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
stakx merged 4 commits into
devlooped:masterfrom
ishatalkin:AmbiguousMatchException
Oct 17, 2019
Merged

stakx merged 4 commits into
devlooped:masterfrom
ishatalkin:AmbiguousMatchException

Conversation

@ishatalkin
Copy link
Copy Markdown
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.
@stakx stakx left a comment
Copy link
Copy Markdown
Contributor

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!

Comment thread src/Moq/ExpressionExtensions.cs Resolved
@stakx stakx added the bug label Oct 16, 2019
@stakx stakx added this to the 4.13.1 milestone Oct 16, 2019
@ishatalkin
ishatalkin commented Oct 16, 2019 via email
Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Contributor Author

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

@stakx stakx left a comment
Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
stakx commented Oct 17, 2019
Copy link
Copy Markdown
Contributor

🚀

Thank you for contributing, @ishatalkin!

@ishatalkin
ishatalkin deleted the AmbiguousMatchException branch October 23, 2019 07:21
@devlooped devlooped locked and limited conversation to collaborators Sep 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0