How do I mock a method with an open array parameter in PascalMock?
- by Oliver Giesen
I'm currently in the process of getting started with unit testing and mocking for good and I stumbled over the following method that I can't seem to fabricate a working mock implementation for:
function GetInstance(const AIID: TGUID;
out AInstance;
const AArgs: array of const;
const AContextID: TImplContextID = CID_DEFAULT): Boolean;
(TImplContextID is just an alias for Integer)
I thought it would have to look something like this:
function TImplementationProviderMock.GetInstance(
const AIID: TGUID;
out AInstance;
const AArgs: array of const;
const AContextID: TImplContextID): Boolean;
begin
Result := AddCall('GetInstance')
.WithParams([@AIID, AContextID])
.ReturnsOutParams([AInstance])
.ReturnValue;
end;
But the compiler complains about the .ReturnsOutParams([AInstance]) saying "Bad argument type in variable type array constructor.". Also I haven't found a way to specify the open array parameter AArgs at all.
Also, is using the @-notation for the TGUID-typed parameter the right way to go?
Is it possible to mock this method with the current version of PascalMock at all?
Update: I now realize I got the purpose of ReturnsOutParams completely wrong: It's intended to be used for populating the values to be returned when defining the expectations rather than for mocking the call itself. I now think the correct syntax for mocking the out parameter would probably have to look more like this:
function TImplementationProviderMock.GetInstance(
const AIID: TGUID;
out AInstance;
const AArgs: array of const;
const AContextID: TImplContextID): Boolean;
var
lCall: TMockMethod;
begin
lCall := AddCall('GetInstance').WithParams([@AIID, AContextID]);
Pointer(AInstance) := lCall.OutParams[0];
Result := lCall.ReturnValue;
end;
The questions that remain are how to mock the open array parameter AArgs and whether passing the TGUID argument (i.e. a value type) by address will work out...