Einzelnen Beitrag anzeigen

Schokohase
(Gast)

n/a Beiträge
 
#8

AW: IInvokable nachträglich einem Interface hinzufügen

  Alt 6. Sep 2018, 11:27
Wo ist das Problem?

Vorgehen nach Schema F plus der Methode etwas Lben in die Mock-Hülle pusten
Delphi-Quellcode:
program MockMock;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  System.Rtti,
  Delphi.Mocks;

type
  IBar = interface
    ['{BFDEEA1F-182C-4009-93DA-78236C497E2E}']
    procedure Bar( );
  end;

  IFoo = interface
    ['{45486F85-F11C-47B8-A85A-328C8845CA41}']
    function Foo( ): IBar;
  end;

type
{$M+}
  IMirrorBar = interface
    ['{AC275EFC-EED0-46A9-9409-8D11B3A86AF9}']
    procedure Bar( );
  end;
{$M-}

{$M+}
  IMirrorFoo = interface
    ['{B5628A7F-7ECF-4123-A079-0193C8894185}']
    function Foo( ): IBar;
  end;
{$M-}


procedure Test( );
var
  foomock: TMock<IMirrorFoo>;
  foomirror: IMirrorFoo;
  foo: IFoo;
  barmock: TMock<IMirrorBar>;
  barmirror: IMirrorBar;
  bar: IBar;
begin
  barmock := TMock<IMirrorBar>.Create( );
  barmock.Setup.WillExecute( 'Bar', function( const args: TArray<TValue>; const ReturnType: TRttiType ): TValue
    begin
      WriteLn( 'Bar called' );
    end );
  barmirror := barmock;
  bar := IBar( barmirror );

  foomock := TMock<IMirrorFoo>.Create( );
  foomock.Setup.WillExecute( 'Foo', function( const args: TArray<TValue>; const ReturnType: TRttiType ): TValue
    begin
      WriteLn( 'Foo called' );

      Result := TValue.From( bar );
    end );
  foomirror := foomock;
  foo := IFoo( foomirror );

  foo.Foo( ).Bar( );
end;

begin
  try
    Test( );
  except
    on E: Exception do
      Writeln( E.ClassName, ': ', E.Message );
  end;
  ReadLn;
end.
  Mit Zitat antworten Zitat