-
Notifications
You must be signed in to change notification settings - Fork 108
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
Question: How to mock a method that passes a parameter by reference? #88
Comments
Currently there isn't a way to achieve var or out parameter return values with Delphi.Mocks. I have had plans on how to achieve this, just haven't had the time to attempt its implementation. The DoInvoke of the proxy classes needs to handle updating var variables and out variables. Even though the args array is const itself, from memory the values of the variables at each index could be altered to the desired values. These would then be returned to the caller, only when the value was either listed as var, or out. If there is information on the method as to the parameter type we could even protect against this. |
Thanks for the reply. Doesn't sound like any small undertaking to achieve this capability. My best bet would probably be to eliminate my argument passed by reference and have my method just return the data instead; pass the old result value of the method through a separate property/method. Sent from my android device. -----Original Message----- Currently there isn't a way to achieve var or out parameter return values with Delphi.Mocks. I have had plans on how to achieve this, just haven't had the time to attempt its implementation. The DoInvoke of the proxy classes needs to handle updating var variables and out variables. Even though the args array is const itself, from memory the values of the variables at each index could be altered to the desired values. These would then be returned to the caller, only when the value was either listed as var, or out. If there is information on the method as to the parameter type we could even protect against this. You are receiving this because you authored the thread. |
Yes, and it is one of the draw backs for using Delphi.Mocks. Even in C# you have to do something like the following for Moq. int outVariable = 2; The outVariable always gets a hint saying it could be removed as the value is not used. I suspect we would get the same thing in delphi as the compiler has no idea we are intercepting the method call. |
I have a method (function) that has two arguments and returns an integer. The first argument is passed by value and the second by reference (var). I would like to mock what this method should pass back on on the second argument. First, the closest thing I can see to accomplish this is to use WillExecute. However this only passes my method's arguments in as const. I am setting the result of my TExecuteFunc equal to args[0]. I would like to set args[1] equal to some value.
How to mock, or stub, a method that has one or more arguments that are passed by reference? I am wanting to test the argument that is passed by reference. The CUT has a method that that passes an argument by reference.
MockDap.Setup.WillExecute(function (const args : TArray<TValue>; const ReturnType : TRttiType) : TValue begin result := args[0]; args[1] := Expected; end).When.GetDAPData(TheLength, Actual);
Thanks
The text was updated successfully, but these errors were encountered: