Delphi test double library for DUnit / DUnitX
Inspired by VSoftTechnologies/Delphi-Mocks
VSoftTechnologies/Delphi-Mocks is very awesome !
But VSoftTechnologies/Delphi-Mocks is
- Only setup or a expection can not specify one mock method.
- Multiple expection can not specify one mock method.
On the other hand,this library is
- it can specify setup and expection simaltanuously.
- expections can combine using not / or / and operators.
In addition to, This library is supported follow features similar with VSoftTechnologies/Delphi-Mocks
- AAA (Arrange-Act-Assert) style mock.
- Interface instanciation.
-
Tests for this library is useed VSoftTechnologies/DUnitX and ritalin/haxe-should
-
This library is only tested by Delphi XE5.
- Add to source path in project option.
- Add MockTools.ExceptionHandler.DUnitX.pas (for DUnitX) or MockTools.ExceptionHandler.DUnit.pas (for DUnit) to project.
- Add unit: MockTools.Mocks at the uses declative in test fixture.
- Arrange mock object.
- Act it.
- Assert expection.
var
mock: TMock<TSomeObject>;
begin
mock := TMock.Create<TSomeObject>;
mock.Setup.WillReturn('Foo').Expect(Exactry(2)).When.ToString;
mock.Instance.ToString;
mock.Instance.ToString;
mock.VerifyAll;
end;
Hint : interface most activated RTTI. Please, use {$M+}/{$M-} compiler instruction.
{$M+}
type ICounter = interface
procedure CountUp;
function CallCount: integer;
end;
{$M-}
var
mock: TMock<ICounter>;
begin
mock := TMock.Create<ICounter>;
mock.Setup.WillReturn(108).Expect(Exactry(2)).When.CallCount;
mock.Instance.CallCount;
mock.Instance.CallCount;
mock.VerifyAll;
end;
{$M+}
type IShowing = interface
function ToString;
end;
{$M-}
var
mock: TMock<ICounter>;
begin
mock := TMock.Create<ICounter>([IShowing]);
mock.SetUp<IShowing>.WillReturn('Foo').Expect(Only).When.ToString;
mock.Instance<IShowing>.ToString;
mock.VerifyAll;
Hint : SysUtils.Support function allow to switch interface.
var
mock: TMock<ICounter>;
begin
mock := TMock.Create<TSomeObject>;
mock.Stub.WillReturn('Foo').When.ToString;
mock.Instance.ToString;
Hint : Stub also supprts interface switching.