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

Question: How to mock a method that passes a parameter by reference? #88

Open
rpottsoh opened this issue Mar 29, 2016 · 3 comments
Open
Labels

Comments

@rpottsoh
Copy link
Contributor

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

@staticcat
Copy link
Contributor

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.

@rpottsoh
Copy link
Contributor Author

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-----
From: Jason Smith notifications@github.com
To: VSoftTechnologies/Delphi-Mocks Delphi-Mocks@noreply.github.com
Cc: Ryan Potts rpottsoh@gmail.com
Sent: Tue, 29 Mar 2016 7:32 PM
Subject: Re: [VSoftTechnologies/Delphi-Mocks] Question: How to mock a method that passes a parameter by reference? (#88)

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.
Reply to this email directly or view it on GitHub:
#88 (comment)

@staticcat
Copy link
Contributor

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;
myMock.Setup(x => x.FuncWithOutVar(out outVariable)).Returns(true);

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants