How do I mock a method with an open array parameter in PascalMock?

Posted by Oliver Giesen on Stack Overflow See other posts from Stack Overflow or by Oliver Giesen
Published on 2010-05-20T14:30:24Z Indexed on 2010/05/20 16:00 UTC
Read the original article Hit count: 281

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...

© Stack Overflow or respective owner

Related posts about delphi

Related posts about pascalmock