Skip to content
A Open Source Mocking framework for Delphi XE2 or later
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Build Fixed build script. Aug 15, 2013
DUnitXML @ e446bfb Updated DUnitXML submodule reference. Jul 11, 2014
Examples Fixed XE2 compiler bugs, and also removed DUnitX.Utils usage as it sh… Aug 21, 2016
Tests Merge branch 'master' of github.com:VSoftTechnologies/Delphi-Mocks Nov 25, 2018
.gitignore Fixed comparison when using Variant args. Was raising AV when trying … Oct 4, 2013
.gitmodules Updated to include DUnit XML module. Aug 15, 2013
Delphi.Mocks.AutoMock.pas removed windows from uses Dec 10, 2018
Delphi.Mocks.Behavior.pas Adicionado um procedimento novo, para disparar um raise quando os par… Oct 14, 2016
Delphi.Mocks.Expectation.pas
Delphi.Mocks.Helpers.pas Fixed XE2 compiler bugs, and also removed DUnitX.Utils usage as it sh… Aug 21, 2016
Delphi.Mocks.Interfaces.pas
Delphi.Mocks.MethodData.pas removed windows from uses Dec 10, 2018
Delphi.Mocks.ObjectProxy.pas
Delphi.Mocks.ParamMatcher.pas Memory leak bug correction. Oct 13, 2015
Delphi.Mocks.Proxy.TypeInfo.pas removed windows from uses Dec 10, 2018
Delphi.Mocks.Proxy.pas removed windows from uses Dec 10, 2018
Delphi.Mocks.ReturnTypePatch.pas Implementation of AutoMocking. Jul 31, 2015
Delphi.Mocks.Utils.pas
Delphi.Mocks.Validation.pas Automocks except when mocking interface or object is not possible. Aug 5, 2015
Delphi.Mocks.VirtualInterface.pas
Delphi.Mocks.VirtualMethodInterceptor.pas
Delphi.Mocks.WeakReference.pas
Delphi.Mocks.When.pas Major rework of implements functionality. Some tests not working, don… Jul 11, 2014
Delphi.Mocks.inc
Delphi.Mocks.pas
DelphiMocks.groupproj
LICENSE.txt
README.md
Sample1.RES Started working on Stubs.. still need to improve .Utils.GetDefaultValue Sep 8, 2013
Sample1.dpr Fixed XE2 compiler bugs, and also removed DUnitX.Utils usage as it sh… Aug 21, 2016
Sample1.dproj Fixed XE2 compiler bugs, and also removed DUnitX.Utils usage as it sh… Aug 21, 2016
Sample1Main.pas Fixed compiler issue with an example that was using the old It record. Jul 30, 2015

README.md

Delphi Mocks

Delphi Mocks is a simple mocking framework for Delphi XE2 or later. It makes use of RTTI features that are only available in Delphi XE2, however I do hope to be able to get it working with earlier versions of Delphi (2010 or later) at some stage.

Example

unit Delphi.Mocks.Examples.Interfaces;

interface

uses
  SysUtils,
  DUnitX.TestFramework,
  Delphi.Mocks;

type
  {$M+}
  TSimpleInterface = Interface
    ['{4131D033-2D80-42B8-AAA1-3C2DF0AC3BBD}']
    procedure SimpleMethod;
  end;

  TSystemUnderTestInf = Interface
    ['{5E21CA8E-A4BB-4512-BCD4-22D7F10C5A0B}']
    procedure CallsSimpleInterfaceMethod;
  end;
  {$M-}

  TSystemUnderTest = class(TInterfacedObject, TSystemUnderTestInf)
  private
    FInternalInf : TSimpleInterface;
  public
    constructor Create(const ARequiredInf: TSimpleInterface);
    procedure CallsSimpleInterfaceMethod;
  end;

  TMockObjectTests = class
  published
    procedure Simple_Interface_Mock;
  end;

implementation

uses
  Rtti;

{ TMockObjectTests }

procedure TMockObjectTests.Simple_Interface_Mock;
var
  mock : TMock<TSimpleInterface>;
  sutObject : TSystemUnderTestInf;
begin
  //SETUP: Create a mock of the interface that is required by our system under test object.
  mock := TMock<TSimpleInterface>.Create;

  //SETUP: Add a check that SimpleMethod is called atleast once.
  mock.Setup.Expect.AtLeastOnce.When.SimpleMethod;

  //SETUP: Create the system under test object passing an instance of the mock interface it requires.
  sutObject := TSystemUnderTest.Create(mock.Instance);

  //TEST: Call CallsSimpleInterfaceMethod on the system under test.
  sutObject.CallsSimpleInterfaceMethod;

  //VERIFY: That our passed in interface was called at least once when CallsSimpleInterfaceMethod was called.
  mock.Verify('CallsSimpleInterfaceMethod should call SimpleMethod');
end;

{ TSystemUnderTest }

procedure TSystemUnderTest.CallsSimpleInterfaceMethod;
begin
  FInternalInf.SimpleMethod;
end;

constructor TSystemUnderTest.Create(const ARequiredInf: TSimpleInterface);
begin
  FInternalInf := ARequiredInf;
end;

end.
You can’t perform that action at this time.