pio pio
pio pio

Reputation: 794

Checking private fields in unit testing

I have the following interface and class

interface
....
  MyInterface = interface
    ['{079729DE-3E8C-4C1B-80AD-114F0A4CD04A}']
    function MyFunction : integer;
    procedure MyProcedure;
  end;

  TMyClass = class (TInterfacedObject, MyInterface)
  private
    FMyValue : integer;
  public
    function MyFunction : integer;
    procedure MyProcedure;
  end;

implementation
.....
function TMyClass.MyFunction: integer;
var
  aResult : integer;
begin
  { here the value of AResult is changed}
  result := aResult;
end;

procedure TMyClass.MyProcedure;
var
  aValue : integer;
begin
  { here the value of FMyValue is changed}
  { here the value of aValue is changed}
end;

I can check the result of MyFunction easily with check (MyFunction=...) but I don't know how to check the values of FMyValye and aValue at the end of the procedure.

I'd rather avoid to add an extra function in both, interface and class, that retrieves the value of FMyValue and aValue because is used only for testing and not for production.

Upvotes: 0

Views: 191

Answers (2)

David Heffernan
David Heffernan

Reputation: 612954

You don't want to check the value of private members and local variables. They are not part of the externally visible interface and so their values are of no concern.

If you are concerned about these values then your code architecture is incorrectly designed. The appropriate solution is of course to redesign the code.

Of course you could make the private field public, or return the local variable via an out parameter. But you don't want to do that, do you?

Upvotes: 1

gturri
gturri

Reputation: 14599

To be able to test FMyValue without adding a getter, you could make it protected rather than private.

In your test you could then add a derived class which could access this value, to let you check it.

Upvotes: 0

Related Questions