使用 Spring 框架中的 Nullable 类型设置 Delphi DSharp 模拟的最佳方法是什么?我尝试了各种方法,我知道我可能遗漏了一些非常基本的东西,但我就是不知道如何让以下代码工作:
program DSharkMockNullable;
{$APPTYPE CONSOLE}
{$R *.res}
uses
DSharp.Testing.Mock,
Spring,
System.SysUtils;
type
{$M+}
IBaseMock = interface
['{B3311345-3C1F-47E3-8235-57B1BA04E957}']
function GetId: TNullableInteger;
procedure SetId(Value: TNullableInteger);
property Id: TNullableInteger read GetId write SetId;
end;
{$M+}
IMockMe = interface(IBaseMock)
['{07F8F233-E8F5-4743-88C5-97A66BB01E29}']
function GetObjectId: TNullableInteger;
procedure SetObjectId(Value: TNullableInteger);
property ObjectId: TNullableInteger read GetObjectId write SetObjectId;
function GetObjectName: TNullableString;
procedure SetObjectName(Value: TNullableString);
property ObjectName: TNullableString read GetObjectName write SetObjectName;
end;
TMyObject = class
public
function ObjectIdAsString(const AObject: IMockMe): string;
end;
{ TMyObject }
function TMyObject.ObjectIdAsString(const AObject: IMockMe): string;
var
LId: Integer;
LObjectId: Integer;
LObjectName: string;
begin
LId := AObject.Id; // ***** FAILS HERE with an "Invalid Typecast" error
LObjectId := AObject.ObjectId;
LObjectName := AObject.ObjectName;
Result := Format('Id: %d', [LId]);
Result := Format('Object Id: %d', [LObjectId]);
Result := Format('Object Name: %s', [LObjectName]);
end;
var
LMock: Mock<IMockMe>;
LObject: TMyObject;
begin
try
LMock.Setup.WillReturn(123).Any.WhenCalling.Id;
LMock.Setup.WillReturn(456).Any.WhenCalling.ObjectId;
LMock.Setup.WillReturn('blahblah').Any.WhenCalling.ObjectName;
LObject := TMyObject.Create;
Writeln(LObject.ObjectIdAsString(LMock.Instance));
LObject.Free;
Readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
有什么想法或建议会很棒吗?我正在使用德尔福 XE5。谢谢。瑞克。