Skip to content

Quick Start Guide

vincentparrett edited this page Sep 18, 2011 · 11 revisions

##Delphi-Mocks

Delphi-Mocks is simple mocking framework for Delphi. It's intended to be used in conjunction with unit testing. The api is fluent where possible, and strongly typed where possible.

###RTTI on interfaces

Delph-Mocks currently only supports mocking interfaces, and is only supported in Delphi XE2. An interface must have Runtime Type Information (RTTI), to enable RTTI on an interface, add the {$M+} compiler directive before it.

{$M+}
IFoo = interface
 procedure Bar;
end;

###Creating the Mock.

We make extensive use of Generics in the framework. To create the mock, we use the TInterfaceMock<T>.Create class function where T is the interface we want to mock. Since delphi generics

procedure Test;
var
  mock : TInterfaceMock<IFoo>; //our mock object
begin
  //Create our mock
  mock := TInterfaceMock<IFoo>.Create;
 ...
end;

###Setting up the Mock behavior.

The behavior of the Mock is controlled by the TInterfaceMock.Setup method. This method returns an ISetup reference, where we can control what the return values of methods will be.

//We use the Setup to configure our expected behaviour rules and to verify //that those expectations were met. //We use the Setup to configure our expected behaviour rules and to verify //that those expectations were met. ISetup = interface ['{D6B21933-BF51-4937-877E-51B59A3B3268}'] function GetBehaviorMustBeDefined : boolean; procedure SetBehaviorMustBeDefined(const value : boolean);

  //Set Expectations for methods
  function Expect : IExpect<T>;

// //set a required order for methods, e.g A must have been called before B // function Before(const AMethodName : string) : ISetup; // // //set a required order for methods, e.g A must have been called After B // function After(const AMethodName : string) : ISetup;

  //set the return value for a method when called with the parameters specified on the When
  function WillReturn(const value : TValue) : IWhen<T>;

  //Will exedute the func when called with the specified parameters
  function WillExecute(const func : TExecuteFunc) : IWhen<T>;overload;

  //will always execute the func no matter what parameters are specified.
  procedure WillExecute(const AMethodName : string; const func : TExecuteFunc);overload;

  //set the default return value for a method when it is called with parameter values we
  //haven't specified
  procedure WillReturnDefault(const AMethodName : string; const value : TValue);

  //set the Exception class that will be raised when the method is called with the parmeters specified
  function WillRaise(const exceptionClass : ExceptClass; const message : string = '') : IWhen<T>;overload;

  //This method will always raise an exception.. this behavior will trump any other defined behaviors
  procedure WillRaise(const AMethodName : string; const exceptionClass : ExceptClass; const message : string = '');overload;

  //If true, calls to methods for which we have not defined a behavior will cause verify to fail.
  property BehaviorMustBeDefined : boolean read GetBehaviorMustBeDefined write SetBehaviorMustBeDefined;
end;

###Setting our expectations of how the Mock will be used.

###Using the Mock.

Clone this wiki locally