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
Parameter is invalid in Protected().SetupSet() method #1184
Comments
Thanks for reporting these two issues, @jerviscui. I'll look into it. |
Regarding the first issue:
The cause for this appears to be that the second parameter to Therefore, your second setup essentially shadows the former; it is matched for both calls. This doesn't make sense to me and looks like a bug: why take a |
Regarding the second issue:
Turns out we have the same problem here, the provided |
Suggested code changes (click to expand)Change - private static Expression ToExpressionArg(ParameterInfo paramInfo, object arg)
+ private static Expression ToExpressionArg(object arg, Type paramType)
{
...
- return Expression.Constant(arg, paramInfo.ParameterType);
+ return Expression.Constant(arg, paramType);
} Adjust - yield return ToExpressionArg(args[i], methodParams[i]);
+ yield return ToExpressionArg(methodParams[i], args[i]); In order to fix the first issue, modify - var expression = GetSetterExpression(property, ItExpr.IsAny<TProperty>());
+ var expression = GetSetterExpression(property, ToExpressionArg(value, property.PropertyType)); In order to fix the second issue, modify - var expression = GetSetterExpression(property, ItExpr.IsAny<TProperty>());
+ var expression = GetSetterExpression(property, ToExpressionArg(value, property.PropertyType)); These changes will make two unit tests fail. To me they look nonsensical to begin with and should be changed, too: (Why does this test expect the property to have been set to ( |
Yes, I'm so sorry. I have a mistake. |
No, your code is fine. Your second setup does not shadow the first, because it specifies a different argument. But because Moq currently ignores that argument, the second setup ends up shadowing the first setup anyway. This is an error inside Moq. |
I am very grateful for your guidance and encouragement. I'll try create a PR. I may not be as fast as you, you are the best. |
Just like: mock.Protected().SetupGet<string>("PrivatePropForTest", ItExpr.IsAny<TProperty>()).Returns("value"); and Just an idea of mine. |
Nice to hear you're interested in contributing, @jerviscui. Take your time, we're not in a hurry.
No, I don't think so. Remember that properties are really just some syntactic sugar on top of a group of methods:
(Things are a little different for indexers, which are essentially parameterized properties. |
You're right. My mind was in turmoil yesterday. 😂
|
I have already raised this issue and provided a pull request. @stakx Would you like me to complete this pull request ? @jerviscui If you use MoqProtectedAs as detailed in the quick start. It does not have the bug.
|
@tonyhallett, my apologies. I got stalled on your PRs because there were quite a few of them all at the same time, and I got a little confused what each of them is attempting to do. I was plannig to look at all of them in a single setting, but haven't yet found the time for that... resource-wise, it's far easier for me to deal with a single PR at a time. If your PRs indeed cover the same bugs reported by @jerviscui, then I suggest we proceed with your (@tonyhallett) PRs — OK with you, @jerviscui? |
@jerviscui I'd appreciate it if we proceed with mine as it covers more than just the bug identified in this issue. The pull request took a bit of thought and time to craft. |
Happy weekend! I'm OK, listen to you @stakx . I'll also try to do it, for study. |
Hi, @stakx |
@jerviscui, yes. Using |
Sure, I see. I mean is, why must change method define public ISetupSetter<T, TProperty> SetupSet<TProperty>(string propertyName, object value)
{
...
var expression = GetSetterExpression(property, ToExpressionArg(property.SetMethod.GetParameters()[0], value));
...
} Are there hidden details that I don't know about? |
@jerviscui |
I'll try to keep it brief: Reflection is a complicated beast, and lots of things can go wrong. The less we have to rely on it, the better. In this case, (You may think that looking up a property setter is trivial, but it turns out that it isn't always straightforward; check out e.g. |
OK, get. I have only considered doesn't throw exception, because method is checked by 'Guard.CanWrite()' in front. |
Hello, I found a bug?
In
Protected().SetupSe()
method, parameter "aaa" and "aa" is invalid, because at last thesaved
value is "aaa".In
Protected().VerifySet()
method,ItExpr
is invalid, will throwMoq.MockException
:Help me, pls.
The text was updated successfully, but these errors were encountered: