• Stars
    star
    228
  • Rank 175,267 (Top 4 %)
  • Language Pascal
  • License
    Apache License 2.0
  • Created about 13 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Open Source Mocking framework for Delphi XE2 or later

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.

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

Usage is easy:

  mock.Setup.Expect.Once.When.SimpleMethod(It0.IsAny<Integer>, It1.IsAny<String>);
  mock.Setup.WillReturn(3).When.SimpleFunction(It0.IsEqualTo<String>('hello'));

Class matching

Some more attention should be payed for matching classes. Usage of .IsAny<TMyClass> will not work as might be expected, because nil (which is the default return value of IsAny<T>) 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).

  mock.Setup.Expect.Never.When.ExtendedMethod(It0.IsAny<TMyClass>);
  mock.Setup.Expect.Never.When.ExtendedMethod(It0.IsAny<TMyOtherClass>);

This can easily be solved by using .IsNotNil<TMyClass>:

  mock.Setup.Expect.Never.When.ExtendedMethod(It0.IsNotNil<TMyClass>);
  mock.Setup.Expect.Never.When.ExtendedMethod(It0.IsNotNil<TMyOtherClass>);

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
  System.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.

More Repositories

1

DUnitX

Delphi Unit Test Framework
Pascal
383
star
2

VSoft.CommandLineParser

Simple Command Line Options Parser - part of the DUnitX Project
Pascal
90
star
3

VSoft.Awaitable

Async/Await for Delphi
Pascal
52
star
4

VSoft.Messaging

Simple internal application messaging for Delphi
Pascal
50
star
5

VSoft.WeakReferences

Weak References for delphi
Pascal
29
star
6

DUnit-XML

XML Reporter for DUnit which outputs NUnit compatible XML
Pascal
22
star
7

VSoft.HttpClient

WinHTTP Client for Delphi
Pascal
21
star
8

Delphi-Fluent-JSON

Fluent JSON Writer for Delphi
Pascal
19
star
9

Simple-IoC

A Simple IoC Container for Delphi 2010 or later
Pascal
18
star
10

VSoft.SemanticVersion

Semantic Version Parser for Delphi
Pascal
16
star
11

VSoft.VirtualListView

Owner drawn virtual list view for Delphi
Pascal
13
star
12

VSoft.CancellationToken

This is a simple library for Delphi that provides a CancellationTokenSource and CancellationToken much like those in .NET
Pascal
12
star
13

VSoft.Ulid

Universally Unique Lexicographically Sortable Identifier
Pascal
11
star
14

Delphi-Fluent-XML

Fluent XML writer for Delphi
Pascal
11
star
15

DelphiCodeCoverageExample

An example of how to run code coverage in Delphi
HTML
10
star
16

VSoft.Uri

Simple Uri Parser library for Delphi
Pascal
9
star
17

VSoft.UUIDv7

A Delphi implementation of a UUID v7 Generator
Pascal
8
star
18

VSoft.AntPatterns

Pascal
7
star
19

DPackGen

DPackGen delphi dpk/dproj generator
Pascal
4
star
20

FinalBuilder.Examples

Example FinalBuilder Projects
3
star
21

playground

Just a playground for messing with github api
2
star
22

ChocolateyPackages

VSoft Chocolatey Packages
PowerShell
2
star
23

VSoft.Extensions.Hosting.Halibut

Microsoft.Extensions.Hosting support for Halibut
C#
2
star
24

cdn-images

Repo for VSoft product icons and images
2
star
25

FinalBuilder-VSO

A Team Foundation Build task for running FinalBuilder projects
PowerShell
2
star
26

VSoft.OperationResult

Pascal
2
star
27

Spring4DMirror

A mirror of Spring4D from bitbucket - just for hosting DPM packages for now - this is temporary.
Pascal
1
star
28

Automated-UI-Testing

Automated UI Testing Done Right
C#
1
star