1

I'm using PascalMock (http://sourceforge.net/projects/pascalmock/) to mock various interfaces in my DUnit unit tests.

I am familiar with how to handle parameters and return values, but I don't understand how to code var parameters.

For example, to mock an interfaced version if TIniFile.ReadSections, I have tried:

procedure TIniFileMock.ReadSections(Strings: TStrings);
begin
  AddCall('ReadSections').WithParams([Strings]).ReturnsOutParams([Strings]);
end;

and then set the expectation using:

IniMock.Expects('ReadSections').WithParams([Null])
  .ReturnsOutParams([Sections]);

but this isn't returning the values that I've put into Sections. I've tried various other permutations, but clearly I'm missing something. There seems to be very little in the way of examples on the internet.

What is the correct way of returning var parameters with PascalMock?

Richard A
  • 2,783
  • 2
  • 22
  • 34
  • `TIniFile.ReadSections` doesn't have a `var` parameter. The code you've provided doesn't match the question you claim to want an answer to. – Rob Kennedy May 15 '17 at 13:52
  • Thanks @RobKennedy, I realised that about 10 minutes after I posted it, I must have been having a bit of a 'moment'. They question still stands, but my example is ridiculous. I'll edit the question when I have a moment. – Richard A May 16 '17 at 22:58

1 Answers1

1

You appear to have misunderstood how TIniFile.ReadSections works.

The Strings parameter to this method is not a var parameter but simply an object reference.

You are required to pass in a reference to a TStrings derived object (usually an instance of a TStringList). The method then reads the section names from the INI file and adds them to the TStrings object you supplied:

For example:

sections := TStringList.Create;
try
  ini.ReadSections(sections);

  // Do some work with the 'sections'
  // ..

finally
  sections.Free;
end;

With this clarification I suspect you will need to change your approach to mocking the INI file and certainly your expectations, which are simply wrong. If you call ReadSections with a NIL parameter it will either fail with an access violation or simply do nothing (I suspect the former).

Deltics
  • 22,162
  • 2
  • 42
  • 70
  • Thanks @deltics, I realised that my example was quite wrong. I'll put a better example into the quesition, along with another solution that got me part of the way there. – Richard A May 16 '17 at 22:59