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

mock.Protected() setup methods fail when argument is of type Expression #1188

Closed
tonyhallett opened this issue Jul 22, 2021 · 2 comments · Fixed by #1189
Closed

mock.Protected() setup methods fail when argument is of type Expression #1188

tonyhallett opened this issue Jul 22, 2021 · 2 comments · Fixed by #1189
Milestone

Comments

@tonyhallett
Copy link
Contributor

var mock = new Mock<FooBase>();
var expression = Expression.Constant(1);
mock.Protected().SetupSet<Expression>("ExpressionProperty", expression);

Throws

System.ArgumentException : Expression of type 'System.Int32' cannot be used for parameter of type 'System.Linq.Expressions.Expression' of method 'Void set_ExpressionProperty(System.Linq.Expressions.Expression)'

ToExpressionArg should check the Type and uses Expression.Constant if the argument type is a LambdaExpression / Expression
https://github.com/moq/moq4/blob/9e148f6955a2aef09c6e4a5c117c7c7deef0756e/src/Moq/Protected/ProtectedMock.cs#L520

		private static Expression ToExpressionArg(Type type, object arg)
		{
			if (arg is LambdaExpression lambda)
			{
				return lambda.Body;
			}

			if (arg is Expression expression)
			{
				return expression;
			}

			return Expression.Constant(arg, type);
		}
``
@stakx
Copy link
Contributor

stakx commented Jul 22, 2021

@tonyhallett, could you please provide a minimal but complete repro (one including the declaration of FooBase.ExpressionProperty). Thanks!

@tonyhallett
Copy link
Contributor Author

tonyhallett commented Jul 22, 2021

public class FooBase
{
	protected virtual ConstantExpression ExpressionProperty { get; set; }
}

public class Test {
      [Fact]
      public void This_Throws_But_Should_Not(){
         var mock = new Mock<FooBase>();
         var expression = Expression.Constant(1);
         mock.Protected().SetupSet<Expression>("ExpressionProperty", expression);
      }
}

tonyhallett added a commit to tonyhallett/moq4 that referenced this issue Jul 22, 2021
@stakx stakx changed the title ProtectedMock fails when arguments are of type Expression mock.Protected().SetupSet fails when argument is of type Expression Jul 22, 2021
@stakx stakx removed the needs-repro label Jul 25, 2021
@tonyhallett tonyhallett changed the title mock.Protected().SetupSet fails when argument is of type Expression mock.Protected() setup methods fails when argument is of type Expression Aug 2, 2021
@tonyhallett tonyhallett changed the title mock.Protected() setup methods fails when argument is of type Expression mock.Protected() setup methods fail when argument is of type Expression Aug 2, 2021
stakx added a commit that referenced this issue Aug 3, 2021
…n-type-args

fix ProtectedMock fails when arguments are of type Expression #1188
@stakx stakx added this to the 4.17.0 milestone Aug 3, 2021
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 a pull request may close this issue.

2 participants