如何模擬介面方法呼叫,如procedure foo( var i_ : integer ). 測驗方法區域變數作為 var 引數傳遞,因此測驗必須使用 Arg.IsAny(測驗不訪問它)。結果值與 var 引數的 out 值不同,因為被測驗的方法在回傳結果之前對其進行了一些處理。When測驗中的注釋變體無法編譯。當前編譯但產生一個未定義的值(模擬Executes根本不呼叫,因為 var=pointer 值不匹配)。如何模擬帶有var引數的方法呼叫?
unit Unit1;
interface
uses
DUnitX.TestFramework
, Spring.Mocking
;
type
IMyInterface = interface ( IInvokable )
['{606BA1D8-EAEC-42CB-A774-911628FD2E6C}']
procedure foo( var x_ : integer );
end;
TMyClass = class
private
fMyInterface : IMyInterface;
public
constructor Create( myInterface_ : IMyInterface );
function bar : integer;
end;
[TestFixture]
TMyClassUnitTest = class
public
[Test]
procedure bar;
end;
implementation
constructor TMyClass.Create( myInterface_ : IMyInterface );
begin
inherited Create;
fMyInterface := myInterface_;
end;
function TMyClass.bar : integer;
var
i : integer;
begin
fMyInterface.foo( i );
result := i 1;
end;
procedure TMyClassUnitTest.bar;
var
myInterfaceMock : Mock<IMyInterface>;
myClass : TMyClass;
i : integer;
procedure prepareMyInterfaceFooCall( fooVarValue_ : integer );
var
ii : integer;
begin
ii := 7;
myInterfaceMock.Setup.Executes(
function ( const args_ : TCallInfo ) : TValue
begin
args_[0] := TValue.From<integer>( fooVarValue_ );
end
//).When.foo( Arg.IsAny<integer> );
//).When.foo( integer( Arg.IsAny<integer> ) );
).When.foo( ii );
end;
begin
prepareMyInterfaceFooCall( 5 );
myClass := TMyClass.Create( myInterfaceMock );
try
i := myClass.bar;
finally
FreeAndNIL( myClass );
end;
Assert.AreEqual( 6, i );
end;
end.
uj5u.com熱心網友回復:
1.2.2 不能這樣做,但 2.0 可以(目前正在開發分支)
這是您的代碼的相關更改:
procedure prepareMyInterfaceFooCall(expectedValue: Integer);
begin
myInterfaceMock.Setup.Executes
// put the wildcard matcher because your code passes a non initialized variable
// a matcher on the When() always has priority over any individual parameter matching
.When(Args.Any)
// use the Arg.Ref syntax specifying the return value
.foo(Arg.Ref<Integer>(expectedValue).Return);
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/406498.html
標籤:
上一篇:如何正確測驗Spring4D1.2.2中模擬方法的呼叫號?
下一篇:具有交替列的離子網格
