A Open Source Mocking framework for Delphi XE2 or later
Pascal C++ PHP
Latest commit 9cd468a Jul 1, 2016 @vincentparrett vincentparrett committed on GitHub Merge pull request #102 from AndreasDDi/master
Added TMock.InstanceAsValue<I>
Failed to load latest commit information.
Build Fixed build script. Aug 15, 2013
DUnitXML @ e446bfb Updated DUnitXML submodule reference. Jul 11, 2014
Examples Fixed compiler error I added. Jun 1, 2016
Tests Merge pull request #97 from AndreasDDi/patch-2 Jun 1, 2016
.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 Automock to return proxy as type requested. Aug 5, 2015
Delphi.Mocks.Behavior.pas Handled ParamMatcher issue with undefined order of executions for Params Jul 30, 2015
Delphi.Mocks.Expectation.pas Fixed expectations to use matches as well. Jul 30, 2015
Delphi.Mocks.Helpers.pas Allow TValue to be able to compare TGUIDs Aug 11, 2015
Delphi.Mocks.Interfaces.pas Added param matchers to Expectations Sep 10, 2014
Delphi.Mocks.MethodData.pas Fix some bugs and failed tests related to DUnitX migration. Nov 15, 2015
Delphi.Mocks.ObjectProxy.pas TObjectProxy changes: added possibility of creating instance of objec… Nov 16, 2015
Delphi.Mocks.ParamMatcher.pas Memory leak bug correction. Oct 13, 2015
Delphi.Mocks.Proxy.TypeInfo.pas Add files via upload May 31, 2016
Delphi.Mocks.Proxy.pas Merge pull request #97 from AndreasDDi/patch-2 Jun 1, 2016
Delphi.Mocks.ReturnTypePatch.pas Implementation of AutoMocking. Jul 31, 2015
Delphi.Mocks.Utils.pas Automocks except when mocking interface or object is not possible. Aug 5, 2015
Delphi.Mocks.Validation.pas Automocks except when mocking interface or object is not possible. Aug 5, 2015
Delphi.Mocks.VirtualInterface.pas Fix for XE2 Update 2 Nov 4, 2011
Delphi.Mocks.VirtualMethodInterceptor.pas Fix for XE2 Update 2 Nov 4, 2011
Delphi.Mocks.WeakReference.pas Fiz a implementação com a sugestão do @fabriciocolombo, apenas nas cl… Nov 26, 2015
Delphi.Mocks.When.pas Major rework of implements functionality. Some tests not working, don… Jul 11, 2014
Delphi.Mocks.inc Unnecessary DELPHI_XE7 define removed in few cases May 22, 2016
Delphi.Mocks.pas Added InstanceAsValue<I> Jun 30, 2016
DelphiMocks.groupproj more fixes Sep 9, 2011
LICENSE.txt updated readme and added license Sep 9, 2011
README.md Updated the readme to have a better description Sep 21, 2015
Sample1.RES Started working on Stubs.. still need to improve .Utils.GetDefaultValue Sep 8, 2013
Sample1.dpr Corrected location of the example projects in the sample project. Sep 21, 2015
Sample1.dproj Corrected location of the example projects in the sample project. Sep 21, 2015
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.