-
-
Notifications
You must be signed in to change notification settings - Fork 794
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
The condition determining whether a parameter is 'out' parameter is changed #645
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @koutinho, I haven't yet studied your pull request in detail, but it looks like a legitimate change. I will give a more detailed response later, for now I just have one small refactoring suggestion.
Source/MethodCall.cs
Outdated
@@ -130,7 +130,8 @@ public MethodCall(Mock mock, Condition condition, LambdaExpression originalExpre | |||
var argument = arguments[index]; | |||
if (parameter.ParameterType.IsByRef) | |||
{ | |||
if ((parameter.Attributes & ParameterAttributes.Out) == ParameterAttributes.Out) | |||
if ((parameter.Attributes & ParameterAttributes.Out) == ParameterAttributes.Out && | |||
(parameter.Attributes & ParameterAttributes.In) != ParameterAttributes.In) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition could be simplified to:
if ((parameter.Attributes & (ParameterAttributes.In | ParameterAttributes.Out)) == ParameterAttributes.Out)
@stakx, Thanks for your response and good suggestion. This suggestion realy simplifies the condition and makes it more readable, so i refined the condition for it to conform to your suggestion. And i hope this change will be in the next release, beacuse i faced this problem in my work and currently i have to use my local modifyed copie of Moq and it would be good to use package from NuGet as usual. But i also belive that someone else may as well encounter with this problem and this change would do well for them too. Thanks. |
@koutinho - Merged. Thank you for your contribution! 👍 Your changes will be in the next patch release (4.9.1), which will be released approx. in a few weeks' time. |
Yes, sure, i can describe my problem. Basically my problem is as folllows : The project i am workin on uses Third Party COM library and this library has interface with this signature: So my code has a dependency on this interface and calls OutSteps method. In the unit-test project i have created the mock of this interface so that my class is gonna use this mock, not the real implementation of IDualServer that comes with COM library. So in the one test method i needed to mock this OutSteps method and use callback in it to perfome some actions upon calling it. So i wrote this: |
@koutinho - Thanks for the explanation. And thanks again for contributing! It's great that you didn't just post a bug report, but also took the initiative to look for the cause & provide a bugfix. 😃 I'll likely post a short notice here once 4.9.1 is released. |
@koutinho - I've just pushed Moq 4.10.0 (which includes your improvement) to NuGet. It should be available shortly. |
According to that rules ('Mapping method parameter keywords in C# to IDL's Directional attributes' section) and MSDN description about Directional Attributes, the c# 'out' method parameter keyword maps to only [out] directional attribute. But in the current implementation if the parameter has [in,out] directional attributes the Moq framework is gonna take it as 'out' parameter, which is wrong, because according to the rules in the article and msdn reference i posted above it gotta take it as 'ref' parameter, not the out. This situation may ocuur for example when you try to mock the COM interface, which has a method with [in,out] directional attributes in COM layer. In interop assembly this method wil be presented as 'ref' parameter, but moq framework is gonna see it as 'out' therefore you are not gonna be able to mock this method propperly and use it unit tests. So when determining the 'out' parameter we need to see not only that 'out' directional attribute is present, but that 'In' directional attribute is not present or otherwise it is the 'ref' parameter not the 'out'.