【发布时间】:2021-11-03 08:28:24
【问题描述】:
我有一个接口函数,它有一个常量数组和一个匿名函数作为参数:
TCodeword = array[0..7] of Char;
TIntFunc = reference to function: Integer;
IMyInterface = interface(IInvokable)
function DoSomething(const codeword: TCodeword; func: TIntFunc): Boolean;
end;
我想模拟那个接口来测试一个正在使用它的对象:
function IntFunc: Integer;
begin
Result := 5;
end;
procedure Test;
var
MyInterfaceMock: Mock<IMyInterface>;
MyInterface: IMyInterface;
begin
MyInterfaceMock := Mock<IMyInterface>.Create(TMockbehavior.Strict);
MyInterfaceMock.Setup.Returns(true).When.DoSomething(arg.IsAny<TCodeword>, arg.IsAny<TIntFunc>());
MyInterface := MyInterfaceMock;
MyInterface.DoSomething('12345678', IntFunc);
end;
运行时,设置时会引发 ENotSupportedException: ‚Type is not supported: TCodeword‘。 有人可以解释为什么这是不支持的类型吗?如何正确传递未指定的 TCodeword 来模拟该函数?
另外,我尝试在设置中传递显式参数:
procedure Test;
var
MyInterfaceMock: Mock<IMyInterface>;
MyInterface: IMyInterface;
begin
MyInterfaceMock := Mock<IMyInterface>.Create(TMockbehavior.Strict);
MyInterfaceMock.Setup.Returns(true).When.DoSomething('12345678', IntFunc);
MyInterface := MyInterfaceMock;
MyInterface.DoSomething('12345678', IntFunc);
end;
这样它将适用于常量数组,但不适用于匿名函数。我得到一个 EMockException: 'unexpected call of function DoSomething(const codeword: TCodeword; func: TIntFunc): Boolean with arguments: nil, (array)';
我怎样才能做到这一点?我很高兴有任何帮助!
【问题讨论】: