MockSequence.Completed used any more?

48 views
Skip to first unread message

Anders Gustavsson

unread,
Aug 23, 2023, 11:07:04 AM8/23/23
to Spring4D
I did a first try with MockSequence, simply defining an interface to test like this:
  ISequenceTest = interface(IInvokable)
    procedure DoValue(ANumber: integer);
  end;
A class to be tested defined like this:
    TSequenceTestObject = class
    private
      FSequenceTest: ISequenceTest;
    public
      constructor Create(ASequenceTest: ISequenceTest);
      procedure Run(AValueA, AValueB: integer);
    end;  

and implementations like this:
constructor TSequenceTestObject.Create(ASequenceTest: ISequenceTest);
begin
  inherited Create;
  FSequenceTest := ASequenceTest;
end;

procedure TSequenceTestObject.Run(AValueA, AValueB: integer);
begin
  FSequenceTest.DoValue(AValueA);
  FSequenceTest.DoValue(AValueB);
end;

Now if  I declare a test like this:
procedure TMyTestObject.DoSequenceTest;
var
  Sequence: MockSequence;
  obj     : TSequenceTestObject;
  testIfc : Mock<ISequenceTest>;
begin
  Sequence.Reset;
  testIfc := Mock<ISequenceTest>.Create(TMockBehavior.Strict);
  testIfc.Setup(Sequence).Executes.When.DoValue(9);
  testIfc.Setup(Sequence).Executes.When.DoValue(4);
  obj := TSequenceTestObject.Create(testIfc);
  try
    obj.Run(4, 9);
  finally
    obj.Free;
  end;
  Assert.isTrue(Sequence.Completed);
end;


Calling this I never get to the Assert call at the end. There is an exception about unexpected call to DoValue with value 4. Good enough I would say, but the Completed method seems to be out of date?

/Anders
Message has been deleted

Stefan Glienke

unread,
Aug 23, 2023, 11:22:55 AM8/23/23
to Spring4D
When using MockSequence the calls have to happen in the exact order you specified in the expectations. You specified "DoValue is being called with 9 and then with 4". And then in Run you call it with 4 first - of course you will get an exception.
Completed is to ensure that all expected calls have been made - try commenting out the first call in Run and watch the unit test fail in the Assert

Anders Gustavsson

unread,
Aug 24, 2023, 8:06:05 AM8/24/23
to Spring4D
Thanks! Found out that "missed call" case later...
Reply all
Reply to author
Forward
Message has been deleted
0 new messages