Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/VSoftTechnologies/Delphi-Mocks

A Open Source Mocking framework for Delphi XE2 or later
https://github.com/VSoftTechnologies/Delphi-Mocks

delphi dpmpackage mocking-framework

Last synced: about 1 month ago
JSON representation

A Open Source Mocking framework for Delphi XE2 or later

Lists

README

        

# 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. See the example at the bottom of the space for a complete explanation.

# Parameter matching

To match expectations or behavior there is extended parameter matching.

```Pascal
function IsAny() : T ;
function Matches(const predicate: TPredicate) : T;
function IsNotNil : T; overload;
function IsNotNil(const comparer: IEqualityComparer) : T; overload;
function IsEqualTo(const value : T) : T; overload;
function IsEqualTo(const value : T; const comparer: IEqualityComparer) : T; overload;
function IsInRange(const fromValue : T; const toValue : T) : T;
function IsIn(const values : TArray) : T; overload;
function IsIn(const values : TArray; const comparer: IEqualityComparer) : T; overload;
function IsIn(const values : IEnumerable) : T; overload;
function IsIn(const values : IEnumerable; const comparer: IEqualityComparer) : T; overload;
function IsNotIn(const values : TArray) : T; overload;
function IsNotIn(const values : TArray; const comparer: IEqualityComparer) : T; overload;
function IsNotIn(const values : IEnumerable) : T; overload;
function IsNotIn(const values : IEnumerable; const comparer: IEqualityComparer) : T; overload;
function IsRegex(const regex : string; const options : TRegExOptions = []) : string;
function AreSamePropertiesThat(const Value: T): T;
function AreSameFieldsThat(const Value: T): T;
function AreSameFieldsAndPropertiedThat(const Value: T): T;
```

Usage is easy:

```Pascal
mock.Setup.Expect.Once.When.SimpleMethod(It0.IsAny, It1.IsAny);
mock.Setup.WillReturn(3).When.SimpleFunction(It0.IsEqualTo('hello'));
```

## Class matching
Some more attention should be payed for matching classes. Usage of `.IsAny` will not work as might be expected, because `nil` (which is the default return value of `IsAny`) is always a good match. Therefore the following setup will fail on the second line, because the framework will think that there is already behavior defined (in the first line).

```Pascal
mock.Setup.Expect.Never.When.ExtendedMethod(It0.IsAny);
mock.Setup.Expect.Never.When.ExtendedMethod(It0.IsAny);
```

This can easily be solved by using `.IsNotNil`:

```Pascal
mock.Setup.Expect.Never.When.ExtendedMethod(It0.IsNotNil);
mock.Setup.Expect.Never.When.ExtendedMethod(It0.IsNotNil);
```

# Example

```Pascal
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
System.Rtti;

{ TMockObjectTests }

procedure TMockObjectTests.Simple_Interface_Mock;
var
mock : TMock;
sutObject : TSystemUnderTestInf;
begin
//SETUP: Create a mock of the interface that is required by our system under test object.
mock := TMock.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.
```