More on Spring Mocks Syntax

60 views
Skip to first unread message

Pio Pio

unread,
Feb 23, 2024, 5:01:16 PMFeb 23
to Spring4D
Hello,

How can I restrict the number of times a call of a specific Spring Mock is executed?

I have the following code:

MyOutMockObject := TMyOutMockObject.Create;
  // adding MyOutMockObject some test data

  FSpringMockCheckOrder.Setup.Executes(
    function(const call: TCallInfo): TValue
    begin
      result := TValue.From<boolean>(TRUE);
      call[8] := MyOutMockObject;
    end).When.CheckOrder(MyOutMockObject);

MyOutMockObject2 := TMyOutMockObject.Create;
  // adding MyOutMockObject2 different test data

  FSpringMockCheckOrder.Setup.Executes(
    function(const call: TCallInfo): TValue
    begin
      result := TValue.From<boolean>(TRUE);
      call[8] := MyOutMockObject2;
    end).When.CheckOrder(MyOutMockObject2);

At this stage, I inject FSpringMockCheckOrder in the class I want to test but the data I defined in MyOutMockObject2 is never returned, no matter how many times I call CheckOrder. The data defined in MyOutMockObject is returned all the time. 
In other words, how can I limit the number of executions of a certain mock instance?

Something like:
    FSpringMockCheckOrder.Setup.Executes(
    function(const call: TCallInfo): TValue
    begin
      result := TValue.From<boolean>(TRUE);
      call[8] := MyOutMockObject;
    end).(Times.Exactly(x)).When.CheckOrder(MyOutMockObject);


Many thanks
Alberto

Stefan Glienke

unread,
Feb 28, 2024, 8:14:57 AMFeb 28
to Spring4D
- what do you think call[8] does? Is that a typo because I don't see 9 parameters on the CheckOrder method.
- if you want to restrict the number of calls and also specify different behavior depending on the call then you need to use a MockSequence
- for providing values for out parameters there is an explicit syntax.

So the code will become:

var
  FSpringMockCheckOrder: Mock<ICheckOrder>;
  MyOutMockObject, MyOutMockObject2, obj: TMyOutMockObject;
  Sequence: MockSequence;
begin
  FSpringMockCheckOrder.Behavior := TMockBehavior.Strict;
  MyOutMockObject := TMyOutMockObject.Create;
  FSpringMockCheckOrder.Setup(Sequence).Returns(True).When.CheckOrder(Arg.Ref<TMyOutMockObject>(MyOutMockObject).Return);
  MyOutMockObject2 := TMyOutMockObject.Create;
  FSpringMockCheckOrder.Setup(Sequence).Returns(True).When.CheckOrder(Arg.Ref<TMyOutMockObject>(MyOutMockObject2).Return);

  Writeln(FSpringMockCheckOrder.Instance.CheckOrder(obj));
  Writeln(obj = MyOutMockObject);
  Writeln(FSpringMockCheckOrder.Instance.CheckOrder(obj));
  Writeln(obj = MyOutMockObject2);
  try
    FSpringMockCheckOrder.Instance.CheckOrder(obj); // this will raise as the 2 expected calls already were performed
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Pio Pio

unread,
Feb 29, 2024, 5:21:08 PMFeb 29
to Spring4D
Hi Stefan,

>  what do you think call[8] does? Is that a typo because I don't see 9 parameters on the CheckOrder method.
You are right. The original method has 9 parameters but when I created the simplified version of it to be posted here I forgot to amend the 9 to, say, 1 and add one parameter to CheckOrder

Thank you for the comprehensive explanation and example.

Alberto
Reply all
Reply to author
Forward
0 new messages