Unit testing 如何使用DUnit模拟Spring4D事件
我正在努力用DUnit成功模拟Spring4d活动 事实上,我更多的是在模仿一个事件的模仿 这是基本结构Unit testing 如何使用DUnit模拟Spring4D事件,unit-testing,delphi,dunit,spring4d,Unit Testing,Delphi,Dunit,Spring4d,我正在努力用DUnit成功模拟Spring4d活动 事实上,我更多的是在模仿一个事件的模仿 这是基本结构 TMyObject --EventContainer--> TMock<IEventContainer> --Event--> TMock<IEvent> 我测试了我能想到的每一种可能性。我要么得到AVs要么得到无效的强制转换。我把源代码放在下面。如果有人能帮我完成这项工作,那真是太棒了 program Project2; {$APPTYPE CONS
TMyObject --EventContainer--> TMock<IEventContainer> --Event--> TMock<IEvent>
我测试了我能想到的每一种可能性。我要么得到AVs要么得到无效的强制转换。我把源代码放在下面。如果有人能帮我完成这项工作,那真是太棒了
program Project2;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
DUnitTestRunner,
Spring.Events,
Spring,
Classes,
TestFramework,
Delphi.Mocks;
//Unit1 in 'Unit1.pas';
type
{$M+}
IMyEvent = interface(IEvent<TNotifyEvent>)
procedure Add(const handler: TMethod);
end;
{$M-}
{$M+}
IMyEventMock = interface(IMyEvent)
procedure Add(const handler: TMethod);
end;
{$M-}
{$M+}
IEventContainer = interface(IInterface)
function GetEvent: IMyEvent;
procedure SetEvent(const Value: IMyEvent);
property Event: IMyEvent
read GetEvent
write SetEvent;
end;
{$M-}
{$M+}
ITestEventContainer = interface(IEventContainer)
function GetEvent: TMock<IMyEvent>;
procedure SetEvent(const Value: TMock<IMyEvent>);
property Event: TMock<IMyEvent>
read GetEvent
write SetEvent;
end;
{$M-}
{$REGION 'TEventContainer'}
TEventContainer = class(TInterfacedObject, IEventContainer)
private
FAEvent: IMyEvent;
function GetEvent: IMyEvent;
procedure SetEvent(const Value: IMyEvent);
public
property Event: IMyEvent
read GetEvent
write SetEvent;
end;
{$ENDREGION}
{$REGION 'TMyObject'}
TMyObject = class(TObject)
private
FEventContainer: IEventContainer;
function GetEventContainer: IEventContainer;
procedure SetEventContainer(const Value: IEventContainer);
public
property EventContainer: IEventContainer
read GetEventContainer
write SetEventContainer;
end;
{$ENDREGION}
{$REGION 'TMyObjectTest'}
TMyObjectTest = class(TTestCase)
strict private
FMyObject: TMyObject;
FMyEventContainerMock: TMock<IEventContainer>;
FMyTestEventContainerMock: TMock<ITestEventContainer>;
FEventMock: TMock<IMyEventMock>;
public
procedure SetUp; override;
procedure TearDown; override;
published
procedure Test_InstanceAsValue;
procedure Test_Value_Make;
procedure Test_Value_From;
procedure Test_Value_From_Instance;
procedure Test_Value_From_Variant;
procedure Test_Value_From_Variant_Instance;
procedure Test_Mocked_Container_Value_Make;
procedure Test_Mocked_Container_Value_From;
procedure Test_Mocked_Container_Value_From_Instance;
procedure Test_Mocked_Container_Value_From_Variant;
procedure Test_Mocked_Container_Value_From_Variant_Instance;
end;
{$ENDREGION}
{$REGION 'TEventContainer'}
function TEventContainer.GetEvent: IMyEvent;
begin
Result := FAEvent;
end;
procedure TEventContainer.SetEvent(const Value: IMyEvent);
begin
FAEvent := Value;
end;
{$ENDREGION}
{$REGION 'TMyObject'}
function TMyObject.GetEventContainer: IEventContainer;
begin
Result := FEventContainer;
end;
procedure TMyObject.SetEventContainer(const Value: IEventContainer);
begin
FEventContainer := Value;
end;
{$ENDREGION}
{$REGION 'TMyObjectTest'}
procedure TMyObjectTest.SetUp;
begin
inherited;
FMyObject := TMyObject.Create;
FMyEventContainerMock := TMock<IEventContainer>.Create;
FMyObject.EventContainer := FMyEventContainerMock;
end;
procedure TMyObjectTest.TearDown;
begin
inherited;
FMyObject.Free;
FMyObject := nil;
end;
procedure TMyObjectTest.Test_Value_Make;
var aValue : TValue;
begin
FEventMock := TMock<IMyEventMock>.Create;
TValue.Make(@FEventMock, TypeInfo(IMyEventMock), aValue);
FMyEventContainerMock.SetUp.WillReturnDefault('GetEvent', aValue);
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_InstanceAsValue;
begin
FEventMock := TMock<IMyEventMock>.Create;
FMyEventContainerMock.SetUp.WillReturnDefault('GetEvent', FEventMock.InstanceAsValue);
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Mocked_Container_Value_From;
begin
FMyTestEventContainerMock := TMock<ITestEventContainer>.Create;
FMyObject.EventContainer := FMyTestEventContainerMock;
FEventMock := TMock<IMyEventMock>.Create;
FMyTestEventContainerMock.SetUp.WillReturnDefault('GetEvent', FEventMock.InstanceAsValue);
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Mocked_Container_Value_From_Instance;
begin
FMyTestEventContainerMock := TMock<ITestEventContainer>.Create;
FMyObject.EventContainer := FMyTestEventContainerMock;
FEventMock := TMock<IMyEventMock>.Create;
FMyTestEventContainerMock.SetUp.WillReturnDefault('GetEvent', TValue.From(FEventMock));
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Mocked_Container_Value_From_Variant;
begin
FMyTestEventContainerMock := TMock<ITestEventContainer>.Create;
FMyObject.EventContainer := FMyTestEventContainerMock;
FEventMock := TMock<IMyEventMock>.Create;
FMyTestEventContainerMock.SetUp.WillReturnDefault('GetEvent', TValue.FromVariant(FEventMock));
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Mocked_Container_Value_From_Variant_Instance;
begin
FMyTestEventContainerMock := TMock<ITestEventContainer>.Create;
FMyObject.EventContainer := FMyTestEventContainerMock;
FEventMock := TMock<IMyEventMock>.Create;
FMyTestEventContainerMock.SetUp.WillReturnDefault('GetEvent', TValue.FromVariant(FEventMock.Instance));
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Mocked_Container_Value_Make;
var aValue : TValue;
begin
FMyTestEventContainerMock := TMock<ITestEventContainer>.Create;
FMyObject.EventContainer := FMyTestEventContainerMock;
FEventMock := TMock<IMyEventMock>.Create;
TValue.Make(@aValue, TypeInfo(TMock<IMyEventMock>), aValue);
FMyTestEventContainerMock.SetUp.WillReturnDefault('GetEvent', aValue);
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Value_From;
begin
FEventMock := TMock<IMyEventMock>.Create;
FMyEventContainerMock.SetUp.WillReturnDefault('GetEvent', TValue.From(FEventMock));
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Value_From_Instance;
begin
FEventMock := TMock<IMyEventMock>.Create;
FMyEventContainerMock.SetUp.WillReturnDefault('GetEvent', TValue.From(FEventMock.Instance));
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Value_From_Variant;
begin
FEventMock := TMock<IMyEventMock>.Create;
FMyEventContainerMock.SetUp.WillReturnDefault('GetEvent', TValue.FromVariant(FEventMock));
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Value_From_Variant_Instance;
begin
FEventMock := TMock<IMyEventMock>.Create;
FMyEventContainerMock.SetUp.WillReturnDefault('GetEvent', TValue.FromVariant(FEventMock.Instance));
FMyObject.EventContainer.Event;
end;
begin
RegisterTest(TMyObjectTest.Suite);
try
DUnitTestRunner.RunRegisteredTests;
ReadLn;
except
on E: Exception do
begin
Writeln(E.ClassName, ': ', E.Message);
ReadLn;
end;
end;
end.
程序项目2;
{$APPTYPE控制台}
{$R*.res}
使用
System.SysUtils,
杜尼特斯特兰纳,
春天,事件,
春天
班级,
测试框架,
Delphi.Mocks;
//“Unit1.pas”中的Unit1;
类型
{$M+}
IMyEvent=接口(IEvent)
过程添加(常量处理程序:TMethod);
结束;
{$M-}
{$M+}
IMyEventMock=接口(IMyEvent)
过程添加(常量处理程序:TMethod);
结束;
{$M-}
{$M+}
IEventContainer=接口(I接口)
函数GetEvent:IMyEvent;
程序SetEvent(常量值:IMyEvent);
属性事件:IMyEvent
读取GetEvent
写入设置事件;
结束;
{$M-}
{$M+}
ITestEventContainer=接口(IEventContainer)
函数GetEvent:TMock;
过程设置事件(常数值:TMock);
属性事件:TMock
读取GetEvent
写入设置事件;
结束;
{$M-}
{$REGION'TEventContainer'}
TEventContainer=类别(TInterfacedObject,IEventContainer)
私有的
FAEvent:IMyEvent;
函数GetEvent:IMyEvent;
程序SetEvent(常量值:IMyEvent);
公众的
属性事件:IMyEvent
读取GetEvent
写入设置事件;
结束;
{$ENDREGION}
{$REGION'TMyObject'}
TMyObject=类(ToObject)
私有的
FEventContainer:IEventContainer;
函数GetEventContainer:IEventContainer;
过程SetEventContainer(const值:IEventContainer);
公众的
属性EventContainer:IEventContainer
读取GetEventContainer
写入SetEventContainer;
结束;
{$ENDREGION}
{$REGION'TMyObjectTest'}
TMyObjectTest=类(TTestCase)
严格保密
FMyObject:TMyObject;
FMyEventContainerMock:TMock;
FMyTestEventContainerMock:TMock;
FEventMock:TMock;
公众的
程序设置;推翻
程序拆卸;推翻
出版
程序测试值;
程序测试值;
程序测试值;
来自实例的程序测试值;
来自变量的程序测试值;
来自变量实例的程序测试值;
程序测试\u模拟\u容器\u值\u制作;
程序测试\u模拟\u容器\u值\u来源;
程序测试\u模拟\u容器\u值\u来自\u实例;
程序测试\u模拟\u容器\u值\u来自\u变量;
程序测试\u模拟\u容器\u值\u来自\u变量\u实例;
结束;
{$ENDREGION}
{$REGION'TEventContainer'}
函数TEventContainer.GetEvent:IMyEvent;
开始
结果:=FAEvent;
结束;
过程TEventContainer.SetEvent(常量值:IMyEvent);
开始
FAEvent:=值;
结束;
{$ENDREGION}
{$REGION'TMyObject'}
函数TMyObject.GetEventContainer:IEventContainer;
开始
结果:=FEventContainer;
结束;
过程TMyObject.SetEventContainer(常量值:IEventContainer);
开始
FEventContainer:=值;
结束;
{$ENDREGION}
{$REGION'TMyObjectTest'}
程序TMyObjectTest.SetUp;
开始
继承;
FMyObject:=TMyObject.Create;
FMyEventContainerMock:=TMock.Create;
FMyObject.EventContainer:=FMyEventContainerMock;
结束;
程序TMyObjectTest.TearDown;
开始
继承;
FMyObject.Free;
FMyObject:=零;
结束;
程序TMyObjectTest.Test\u Value\u Make;
var aValue:TValue;
开始
FEventMock:=TMock.Create;
TValue.Make(@FEventMock,TypeInfo(IMyEventMock),aValue);
FMyEventContainerMock.SetUp.WillReturnDefault('GetEvent',aValue);
FMyObject.EventContainer.Event;
结束;
程序TMyObjectTest.Test_值;
开始
FEventMock:=TMock.Create;
FMyEventContainerMock.SetUp.WillReturnDefault('GetEvent',FEventMock.InstanceValue);
FMyObject.EventContainer.Event;
结束;
程序TMyObjectTest.Test_模拟_容器_值_来源;
开始
FMyTestEventContainerMock:=TMock.Create;
FMyObject.EventContainer:=fmytestventContainerMock;
FEventMock:=TMock.Create;
FMyTestEventContainerMock.SetUp.WillReturnDefault('GetEvent',FEventMock.InstanceValue);
FMyObject.EventContainer.Event;
结束;
程序TMyObjectTest.Test_mock_Container_Value_From_Instance;
开始
FMyTestEventContainerMock:=TMock.Create;
FMyObject.EventContainer:=fmytestventContainerMock;
FEventMock:=TMock.Create;
FMyTestEventContainerMock.SetUp.WillReturnDefault('GetEvent',TValue.From(FEventMock));
FMyObject.EventContainer.Event;
结束;
程序TMyObjectTest.Test_模拟_容器_值_来自_变量;
开始
FMyTestEventContainerMock:=TMock.Create;
FMyObject.EventContainer:=fmytestventContainerMock;
FEventMock:=TMock.Create;
FMyTestEventContainerMock.SetUp.WillReturnDefault('GetEvent',TValue.FromVariant(FEventMock));
FMyObject.EventContainer.Event;
结束;
程序TMyObjectTest.Test_mock_Container_Value_From_Variant_Instance;
开始
FMyTestEventContainerMock:=TMock.Create;
FMyObject.EventContainer:=fmytestventContainerMock;
FEventMock:=TMock.Create;
FMyTestEventContainerMock.SetUp.WillReturnDefault('GetEvent',TValue.FromVariant(FEventMock.Instance));
FMyObject.EventContainer.Event;
结束;
程序TMyObjectTest.Test_Mocked_Container_Value_Make;
var aValue:TValue;
开始
FMyTestEventContainerMock:=TMock.Create;
FMyObject.EventContainer:=fmytestventContainerMock;
FEventMock:=TMock.Create;
TValue.Make(@aValue,TypeInfo(TMock),aValue);
FMyTestEventContainerMock.SetUp.WillReturnDefault('GetEvent',aValue);
FMyObject.EventContainer.Event;
结束;
程序TMyObjectTest.Test\U值\U Fr
program Project2;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
DUnitTestRunner,
Spring.Events,
Spring,
Classes,
TestFramework,
Delphi.Mocks;
//Unit1 in 'Unit1.pas';
type
{$M+}
IMyEvent = interface(IEvent<TNotifyEvent>)
procedure Add(const handler: TMethod);
end;
{$M-}
{$M+}
IMyEventMock = interface(IMyEvent)
procedure Add(const handler: TMethod);
end;
{$M-}
{$M+}
IEventContainer = interface(IInterface)
function GetEvent: IMyEvent;
procedure SetEvent(const Value: IMyEvent);
property Event: IMyEvent
read GetEvent
write SetEvent;
end;
{$M-}
{$M+}
ITestEventContainer = interface(IEventContainer)
function GetEvent: TMock<IMyEvent>;
procedure SetEvent(const Value: TMock<IMyEvent>);
property Event: TMock<IMyEvent>
read GetEvent
write SetEvent;
end;
{$M-}
{$REGION 'TEventContainer'}
TEventContainer = class(TInterfacedObject, IEventContainer)
private
FAEvent: IMyEvent;
function GetEvent: IMyEvent;
procedure SetEvent(const Value: IMyEvent);
public
property Event: IMyEvent
read GetEvent
write SetEvent;
end;
{$ENDREGION}
{$REGION 'TMyObject'}
TMyObject = class(TObject)
private
FEventContainer: IEventContainer;
function GetEventContainer: IEventContainer;
procedure SetEventContainer(const Value: IEventContainer);
public
property EventContainer: IEventContainer
read GetEventContainer
write SetEventContainer;
end;
{$ENDREGION}
{$REGION 'TMyObjectTest'}
TMyObjectTest = class(TTestCase)
strict private
FMyObject: TMyObject;
FMyEventContainerMock: TMock<IEventContainer>;
FMyTestEventContainerMock: TMock<ITestEventContainer>;
FEventMock: TMock<IMyEventMock>;
public
procedure SetUp; override;
procedure TearDown; override;
published
procedure Test_InstanceAsValue;
procedure Test_Value_Make;
procedure Test_Value_From;
procedure Test_Value_From_Instance;
procedure Test_Value_From_Variant;
procedure Test_Value_From_Variant_Instance;
procedure Test_Mocked_Container_Value_Make;
procedure Test_Mocked_Container_Value_From;
procedure Test_Mocked_Container_Value_From_Instance;
procedure Test_Mocked_Container_Value_From_Variant;
procedure Test_Mocked_Container_Value_From_Variant_Instance;
end;
{$ENDREGION}
{$REGION 'TEventContainer'}
function TEventContainer.GetEvent: IMyEvent;
begin
Result := FAEvent;
end;
procedure TEventContainer.SetEvent(const Value: IMyEvent);
begin
FAEvent := Value;
end;
{$ENDREGION}
{$REGION 'TMyObject'}
function TMyObject.GetEventContainer: IEventContainer;
begin
Result := FEventContainer;
end;
procedure TMyObject.SetEventContainer(const Value: IEventContainer);
begin
FEventContainer := Value;
end;
{$ENDREGION}
{$REGION 'TMyObjectTest'}
procedure TMyObjectTest.SetUp;
begin
inherited;
FMyObject := TMyObject.Create;
FMyEventContainerMock := TMock<IEventContainer>.Create;
FMyObject.EventContainer := FMyEventContainerMock;
end;
procedure TMyObjectTest.TearDown;
begin
inherited;
FMyObject.Free;
FMyObject := nil;
end;
procedure TMyObjectTest.Test_Value_Make;
var aValue : TValue;
begin
FEventMock := TMock<IMyEventMock>.Create;
TValue.Make(@FEventMock, TypeInfo(IMyEventMock), aValue);
FMyEventContainerMock.SetUp.WillReturnDefault('GetEvent', aValue);
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_InstanceAsValue;
begin
FEventMock := TMock<IMyEventMock>.Create;
FMyEventContainerMock.SetUp.WillReturnDefault('GetEvent', FEventMock.InstanceAsValue);
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Mocked_Container_Value_From;
begin
FMyTestEventContainerMock := TMock<ITestEventContainer>.Create;
FMyObject.EventContainer := FMyTestEventContainerMock;
FEventMock := TMock<IMyEventMock>.Create;
FMyTestEventContainerMock.SetUp.WillReturnDefault('GetEvent', FEventMock.InstanceAsValue);
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Mocked_Container_Value_From_Instance;
begin
FMyTestEventContainerMock := TMock<ITestEventContainer>.Create;
FMyObject.EventContainer := FMyTestEventContainerMock;
FEventMock := TMock<IMyEventMock>.Create;
FMyTestEventContainerMock.SetUp.WillReturnDefault('GetEvent', TValue.From(FEventMock));
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Mocked_Container_Value_From_Variant;
begin
FMyTestEventContainerMock := TMock<ITestEventContainer>.Create;
FMyObject.EventContainer := FMyTestEventContainerMock;
FEventMock := TMock<IMyEventMock>.Create;
FMyTestEventContainerMock.SetUp.WillReturnDefault('GetEvent', TValue.FromVariant(FEventMock));
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Mocked_Container_Value_From_Variant_Instance;
begin
FMyTestEventContainerMock := TMock<ITestEventContainer>.Create;
FMyObject.EventContainer := FMyTestEventContainerMock;
FEventMock := TMock<IMyEventMock>.Create;
FMyTestEventContainerMock.SetUp.WillReturnDefault('GetEvent', TValue.FromVariant(FEventMock.Instance));
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Mocked_Container_Value_Make;
var aValue : TValue;
begin
FMyTestEventContainerMock := TMock<ITestEventContainer>.Create;
FMyObject.EventContainer := FMyTestEventContainerMock;
FEventMock := TMock<IMyEventMock>.Create;
TValue.Make(@aValue, TypeInfo(TMock<IMyEventMock>), aValue);
FMyTestEventContainerMock.SetUp.WillReturnDefault('GetEvent', aValue);
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Value_From;
begin
FEventMock := TMock<IMyEventMock>.Create;
FMyEventContainerMock.SetUp.WillReturnDefault('GetEvent', TValue.From(FEventMock));
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Value_From_Instance;
begin
FEventMock := TMock<IMyEventMock>.Create;
FMyEventContainerMock.SetUp.WillReturnDefault('GetEvent', TValue.From(FEventMock.Instance));
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Value_From_Variant;
begin
FEventMock := TMock<IMyEventMock>.Create;
FMyEventContainerMock.SetUp.WillReturnDefault('GetEvent', TValue.FromVariant(FEventMock));
FMyObject.EventContainer.Event;
end;
procedure TMyObjectTest.Test_Value_From_Variant_Instance;
begin
FEventMock := TMock<IMyEventMock>.Create;
FMyEventContainerMock.SetUp.WillReturnDefault('GetEvent', TValue.FromVariant(FEventMock.Instance));
FMyObject.EventContainer.Event;
end;
begin
RegisterTest(TMyObjectTest.Suite);
try
DUnitTestRunner.RunRegisteredTests;
ReadLn;
except
on E: Exception do
begin
Writeln(E.ClassName, ': ', E.Message);
ReadLn;
end;
end;
end.
Test_InstanceAsValue;
Test_Value_From_Instance;
Test_Mocked_Container_Value_From;
var
container: TContainer;
event: IMyEvent;
begin
container := TContainer.Create;
container.AddExtension<TAutoMockExtension>;
try
FMyObject.EventContainer := container.Resolve<ITestEventContainer>;
event := FMyObject.EventContainer.Event;
event.Add(nil);
finally
container.Free;
end;
end;
var
container: TContainer;
event: IMyEvent;
begin
container := TContainer.Create;
container.AddExtension<TAutoMockExtension>;
container.RegisterType<TMyObject>.InjectProperty('EventContainer');
container.Build;
try
FMyObject := container.Resolve<TMyObject>;
event := FMyObject.EventContainer.Event;
event.Add(nil);
finally
container.Free;
end;
end;
program Project2;
{$APPTYPE CONSOLE}
uses
Classes,
SysUtils,
DUnitTestRunner,
TestFramework,
Spring.Events,
Spring,
Spring.Container,
Spring.Container.Registration,
Spring.Container.AutoMockExtension,
Spring.Mocking;
type
IMyEvent = IEvent<TNotifyEvent>;
IEventContainer = interface(IInvokable)
function GetEvent: IMyEvent;
procedure SetEvent(const Value: IMyEvent);
property Event: IMyEvent read GetEvent write SetEvent;
end;
TMyObject = class(TObject)
private
FEventContainer: IEventContainer;
public
property EventContainer: IEventContainer read FEventContainer write FEventContainer;
end;
TAutoMockingTestCase<T: class> = class(TTestCase)
protected
fContainer: TContainer;
fSUT: T;
procedure SetUp; overload; override;
procedure TearDown; override;
procedure SetUp(const registration: TRegistration<T>); reintroduce; overload; virtual;
end;
TMyTest = class(TAutoMockingTestCase<TMyObject>)
protected
procedure SetUp(const registration: TRegistration<TMyObject>); override;
published
procedure Test_EventAdd;
end;
procedure TAutoMockingTestCase<T>.SetUp(const registration: TRegistration<T>);
begin
end;
procedure TAutoMockingTestCase<T>.SetUp;
begin
inherited;
fContainer := TContainer.Create;
fContainer.AddExtension<TAutoMockExtension>;
SetUp(fContainer.RegisterType<T>);
fContainer.Build;
fSUT := fContainer.Resolve<T>;
end;
procedure TAutoMockingTestCase<T>.TearDown;
begin
fSUT.Free;
fContainer.Free;
inherited;
end;
procedure TMyTest.SetUp(const registration: TRegistration<TMyObject>);
begin
registration.InjectProperty('EventContainer');
end;
procedure TMyTest.Test_EventAdd;
begin
fSUT.EventContainer.Event.Add(nil);
end;
begin
RegisterTest(TMyTest.Suite);
try
DUnitTestRunner.RunRegisteredTests;
ReadLn;
except
on E: Exception do
begin
Writeln(E.ClassName, ': ', E.Message);
ReadLn;
end;
end;
end.