Delphi Mocks es un marco de burla simple para Delphi XE2 o posterior. Hace uso de características RTTI que solo están disponibles en Delphi XE2. Vea el ejemplo en la parte inferior del espacio para una explicación completa.
Para que coincidan con las expectativas o el comportamiento, hay una coincidencia de parámetros extendidos.
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;El uso es fácil:
mock.Setup.Expect.Once.When.SimpleMethod(It0.IsAny<Integer>, It1.IsAny<String>);
mock.Setup.WillReturn( 3 ).When.SimpleFunction(It0.IsEqualTo<String>( ' hello ' )); Se debe pagar un poco más de atención por las clases coincidentes. El uso de .IsAny<TMyClass> no funcionará como podría esperarse, porque nil (que es el valor de retorno predeterminado de IsAny<T> ) es siempre una buena coincidencia. Por lo tanto, la siguiente configuración fallará en la segunda línea, porque el marco pensará que ya hay un comportamiento definido (en la primera línea).
mock.Setup.Expect.Never.When.ExtendedMethod(It0.IsAny<TMyClass>);
mock.Setup.Expect.Never.When.ExtendedMethod(It0.IsAny<TMyOtherClass>); Esto se puede resolver fácilmente usando .IsNotNil<TMyClass> :
mock.Setup.Expect.Never.When.ExtendedMethod(It0.IsNotNil<TMyClass>);
mock.Setup.Expect.Never.When.ExtendedMethod(It0.IsNotNil<TMyOtherClass>); 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 .