Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works

Random project

mocktool-for-delphi

Delphi test double library for DUnit / DUnitX

View on GitHub

Anyone may press Interesting, signed in or not. It helps others find this project.

Member rating

No ratings yet.

Sign in to rate this project.

README

mocktool-for-delphi

Introduction

Delphi test double library for DUnit / DUnitX

Inspired by VSoftTechnologies/Delphi-Mocks

VSoftTechnologies/Delphi-Mocks is very awesome !

But VSoftTechnologies/Delphi-Mocks is

  • Only setup or a expection can not specify one mock method.
  • Multiple expection can not specify one mock method.

On the other hand,this library is

  • it can specify setup and expection simaltanuously.
  • expections can combine using not / or / and operators.

In addition to, This library is supported follow features similar with VSoftTechnologies/Delphi-Mocks

  • AAA (Arrange-Act-Assert) style mock.
  • Interface instanciation.

Requirement

Usage

Project setup

  • Add to source path in project option.
  • Add MockTools.ExceptionHandler.DUnitX.pas (for DUnitX) or MockTools.ExceptionHandler.DUnit.pas (for DUnit) to project.

Introduction procedures for test fixture

  1. Add unit: MockTools.Mocks at the uses declative in test fixture.
  2. Arrange mock object.
  3. Act it.
  4. Assert expection.

Examples

Object mocking

delphi
var
  mock: TMock<TSomeObject>;
begin
  mock := TMock.Create<TSomeObject>;
  mock.Setup.WillReturn('Foo').Expect(Exactry(2)).When.ToString;
  
  mock.Instance.ToString;
  mock.Instance.ToString;
  
  mock.VerifyAll;
end;

Interface instanciation

Hint : interface most activated RTTI. Please, use {$M+}/{$M-} compiler instruction.

delphi
{$M+}
type ICounter = interface
  procedure CountUp;
  function CallCount: integer;
end;
{$M-}
var
  mock: TMock<ICounter>;
begin
  mock := TMock.Create<ICounter>;
  mock.Setup.WillReturn(108).Expect(Exactry(2)).When.CallCount;
  
  mock.Instance.CallCount;
  mock.Instance.CallCount;
  
  mock.VerifyAll;
end;

Interface switching

delphi
{$M+}
type IShowing = interface
  function ToString;
end;
{$M-}

var
  mock: TMock<ICounter>;
begin
  mock := TMock.Create<ICounter>([IShowing]);
  mock.SetUp<IShowing>.WillReturn('Foo').Expect(Only).When.ToString;
  
  mock.Instance<IShowing>.ToString;
  
  mock.VerifyAll;

Hint : SysUtils.Support function allow to switch interface.

Stub setup

delphi
var
  mock: TMock<ICounter>;
begin
  mock := TMock.Create<TSomeObject>;
  mock.Stub.WillReturn('Foo').When.ToString;
  
  mock.Instance.ToString;

Hint : Stub also supprts interface switching.