我测试了一个类来管理 Lockbox 3。我有以下伪模拟:
unit uMockTPWDDBManager;
interface
uses inifiles,classes,UITypes,uTPLb_Codec;
type
IPWDDBManager = interface
['{51C993FE-D96A-4419-AB80-00D65E16C6F8}']
end;
ICodec = interface
['{B1858F24-5B76-4468-8BD5-55684EA43CCD}']
function GetPassword:string;
procedure SetPassword(const c:string);
property Password:string read GetPassword write SetPassword;
end;
TPWDDBManager=class(TIniFile,IPWDDBManager)
private
FCodec1: ICodec;
FRefCount: Integer;
protected
function QueryInterface(const IID: TGUID; out Obj): Integer; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
public
constructor Create(const FileName: string; Fcodec: ICodec);virtual;
property RefCount: Integer read FRefCount;
end;
TMockCodec1 = class(TCodec,ICodec)
private
function GetPassword:string;
procedure SetPassword(const c:string);
end;
implementation
uses spring.container;
{ TMockCodec1 }
function TMockCodec1.GetPassword: string;
begin
result:=Password;
end;
procedure TMockCodec1.SetPassword(const c: string);
begin
password:=c;
end;
{ TPWDDBManager }
constructor TPWDDBManager.Create(const FileName: string; Fcodec: ICodec);
begin
inherited create(FileName);
FCodec1:=Fcodec;
// FCodec1.Password:= 'ABC';
end;
function TPWDDBManager.QueryInterface(const IID: TGUID; out Obj): Integer;
const
E_NOINTERFACE = $80004002;
begin
if GetInterface(IID, Obj) then
Result := 0
else
Result := E_NOINTERFACE;
end;
function TPWDDBManager._AddRef: Integer;
begin
Inc(FRefCount);
Result := FRefCount;
end;
function TPWDDBManager._Release: Integer;
begin
Dec(FRefCount);
if FRefCount = 0 then
begin
Destroy;
Result := 0;
Exit;
end;
Result := FRefCount;
end;
initialization
GlobalContainer.RegisterComponent<TMockCodec1>.Implements<ICodec>;
GlobalContainer.RegisterComponent<TPWDDBManager>.Implements<IPWDDBManager>;
end.
和以下单元测试:
unit TestuTPWDDBManager;
{
Delphi DUnit Test Case
----------------------
This unit contains a skeleton test case class generated by the Test Case Wizard.
Modify the generated code to correctly setup and call the methods from the unit
being tested.
}
interface
uses TestFramework,uMockTPWDDBManager,IniFiles;
type
// Test methods for class TPWDDBManager
TestTPWDDBManager = class(TTestCase)
strict private
FCodec1: ICodec;
FPWDDBManager: IPWDDBManager;
public
procedure SetUp; override;
published
procedure TestGetPWD;
end;
implementation
uses SysUtils,Spring.container,Spring.Services;
procedure TestTPWDDBManager.SetUp;
begin
GlobalContainer.Build;
FCodec1:=ServiceLocator.GetService<ICodec>;
// FCodec1:=TMockCodec1.Create(nil);
FCodec1.Password:='a';
FPWDDBManager :=TPWDDBManager.Create('',FCodec1);
end;
procedure TestTPWDDBManager.TestGetPWD;
begin
end;
initialization
// Register any test cases with the test runner
RegisterTest(TestTPWDDBManager.Suite);
end.
当我运行测试时,我在线上有一条错误消息FCodec1.Password:='a';
如果我替换FCodec1:=ServiceLocator.GetService<ICodec>;
为FCodec1:=TMockCodec1.Create(nil);
然后没有错误FCodec1.Password:='a';
在这种情况下,ServiceLocator 似乎没有创建 TmockCodec1 类。
我在哪里做错了?