Delphi Mocks
v0.3.1
Delphi Mocks는 Delphi XE2 이상을위한 간단한 조롱 프레임 워크입니다. Delphi XE2에서만 사용할 수있는 RTTI 기능을 사용합니다. 완전한 설명은 공간의 맨 아래에있는 예를 참조하십시오.
기대 또는 행동과 일치하기 위해 확장 된 매개 변수 일치가 있습니다.
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;사용법은 쉽습니다.
mock.Setup.Expect.Once.When.SimpleMethod(It0.IsAny<Integer>, It1.IsAny<String>);
mock.Setup.WillReturn( 3 ).When.SimpleFunction(It0.IsEqualTo<String>( ' hello ' )); 일치하는 클래스에 대해서는 약간의주의를 기울여야합니다. .IsAny<TMyClass> 의 사용량은 예상대로 작동하지 않습니다. nil ( IsAny<T> 의 기본 반환 값)은 항상 좋은 일치이기 때문입니다. 따라서 프레임 워크는 이미 정의 된 동작이 (첫 번째 줄에) 생각하기 때문에 두 번째 줄에서 다음 설정이 실패합니다.
mock.Setup.Expect.Never.When.ExtendedMethod(It0.IsAny<TMyClass>);
mock.Setup.Expect.Never.When.ExtendedMethod(It0.IsAny<TMyOtherClass>); 이것은 .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 .