Skip to content
A Open Source Mocking framework for Delphi XE2 or later
Pascal C++ PHP
Find file
Latest commit 25ff9e3 @vincentparrett vincentparrett Merge pull request #87 from Teloah/typo_fix
Fixed some typos in the comments
Failed to load latest commit information.
Build Fixed build script.
DUnitXML @ e446bfb Updated DUnitXML submodule reference.
Examples Fixed some typos in the comments
Tests Fixed some typos in the comments
.gitignore Fixed comparison when using Variant args. Was raising AV when trying …
.gitmodules Updated to include DUnit XML module.
Delphi.Mocks.AutoMock.pas Automock to return proxy as type requested.
Delphi.Mocks.Behavior.pas Handled ParamMatcher issue with undefined order of executions for Params
Delphi.Mocks.Expectation.pas Fixed expectations to use matches as well.
Delphi.Mocks.Helpers.pas
Delphi.Mocks.Interfaces.pas Added param matchers to Expectations
Delphi.Mocks.MethodData.pas Fix some bugs and failed tests related to DUnitX migration.
Delphi.Mocks.ObjectProxy.pas TObjectProxy changes: added possibility of creating instance of objec…
Delphi.Mocks.ParamMatcher.pas Memory leak bug correction.
Delphi.Mocks.Proxy.TypeInfo.pas Revert "Erro a compilar com o "Emit runtime type information""
Delphi.Mocks.Proxy.pas Fiz a implementação com a sugestão do @fabriciocolombo, apenas nas cl…
Delphi.Mocks.ReturnTypePatch.pas Implementation of AutoMocking.
Delphi.Mocks.Utils.pas Automocks except when mocking interface or object is not possible.
Delphi.Mocks.Validation.pas Automocks except when mocking interface or object is not possible.
Delphi.Mocks.VirtualInterface.pas Fix for XE2 Update 2
Delphi.Mocks.VirtualMethodInterceptor.pas Fix for XE2 Update 2
Delphi.Mocks.WeakReference.pas Fiz a implementação com a sugestão do @fabriciocolombo, apenas nas cl…
Delphi.Mocks.When.pas Major rework of implements functionality. Some tests not working, don…
Delphi.Mocks.inc Conditinal defines for Delphi 10 Seattle added.
Delphi.Mocks.pas Merge pull request #83 from ASiwon/proxy_for_constructor_with_params
DelphiMocks.groupproj more fixes
LICENSE.txt updated readme and added license
README.md Updated the readme to have a better description
Sample1.RES Started working on Stubs.. still need to improve .Utils.GetDefaultValue
Sample1.dpr Corrected location of the example projects in the sample project.
Sample1.dproj Corrected location of the example projects in the sample project.
Sample1Main.pas Fixed compiler issue with an example that was using the old It record.

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.
Something went wrong with that request. Please try again.