-
-
Notifications
You must be signed in to change notification settings - Fork 798
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
Invalid Callback error when Returns argument has return type object #622
Comments
Yes, see the changelog for version 4.8.0-rc1. This was requested in #445 and implemented in #520.
|
Sorry for the somewhat naïve suggestion above. The easiest is if you could add a static void SetupMockProperty<TInterface, TResult>(
Mock<TInterface> mock,
PropertyInfo propertyInfo,
Lazy<TResult> lazyValueProvider) where TInterface : class
{
mock
.Setup(BuildPropertyLambda<TInterface, TResult>(propertyInfo))
.Returns(() => lazyValueProvider.Value);
}
static Expression<Func<TInterface, TResult>> BuildPropertyLambda<TInterface, TResult>(
PropertyInfo propertyInfo)
{
var parameter = Expression.Parameter(typeof(TInterface));
var body = Expression.PropertyOrField(parameter, propertyInfo.Name);
return Expression.Lambda<Func<TInterface, TResult>>(body, parameter);
} If that is not an option, then it's going to get a little more complicated. You have a (I admit that this could (and should) be easier. You'll find that Moq has to pull that kind of trick in its own code base. This would be a lot easier if Moq exposed its internal, non-generic API that the generic API is built upon. But it's what we've got.) |
Please let me know how you'd like to proceed with this issue. If I don't hear back from you, I'll close this in about 2-3 weeks' time. |
I think since this is intentional and permanent behavior, and that there appears to be a work around, we can close this issue. Thanks for the help. |
There appears to be a behavior change or regression with Moq 4.8.2 that did not occur with 4.7.145.
I have a reusable library for creating mocks at runtime based on an interface that the end developer provides. This means that some reflection must be used, and that generic type arguments cannot be used. Here is a simplified demonstration of the behavior:
The
SetupMockProperty
represents the isolation from generic type arguments that is required for this use case. This code works great with 4.7.145, but gets the specified error with 4.8.2.Is this new behavior intentional? And if so is there a different way that this use case can be approached?
The text was updated successfully, but these errors were encountered: