Skip to content

ritalin/mocktool-for-delphi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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

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.

{$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

{$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

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

Hint : Stub also supprts interface switching.

About

Delphi test double library for DUnit / DUnitX

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages