2

DUnit を使用して Spring4d イベントを正常にモックするのに苦労しています。

実際、私はイベントのモックを返すモックをもっとモックしています...

これが基本構造です。

TMyObject --EventContainer--> TMock<IEventContainer> --Event--> TMock<IEvent>

TMyObject にはプロパティ EventContainer があります: IEventContainer

IEventContainer にはプロパティ Event があります: IMyEvent

嘲笑したい

MyObject.EventContainer.Event.Add

考えられるすべての可能性をテストしました。AV または無効なキャストのいずれかを取得します。以下にソースコードを置いておきます。誰かが私がこれを機能させるのを手伝ってくれたら、それは本当に気の利いたものになるでしょう!

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.
4

1 に答える 1

6

まずあなたのアプローチが間違っています。インターフェイスを継承してから追加すると、それ以降に追加{$M+}されたメソッドのメソッド情報のみが含まれます。つまり、親インターフェイスと同じ署名を持つメソッドを追加しても、コードは追加したメソッドではなく親インターフェイス メソッドを呼び出すため、モックは機能しません。

さらに、この場合の DelphiMocks は、インターフェイスからその親型への TValue 変換のバグの被害者です。これはサポートされていません - を参照してくださいRtti.ConvIntf2Intf

IInvokable から継承する IEvent を使用して Spring4D をコンパイルし、そこにメソッド情報を取得して継承を回避することをお勧めします。

これを行うと、次のテストに合格します - 他のすべてのテストは単に間違ったモックに合格しています:

Test_InstanceAsValue;
Test_Value_From_Instance;
Test_Mocked_Container_Value_From;

Spring4D 1.2 では、モック ソリューションにも使用される新しい傍受ライブラリを導入しています。また、コンテナーは自動モックを提供できます。したがって、次のようにテストを記述できます。

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.
于 2015-05-20T13:31:08.870 に答える