我花了很多时间思考单元测试。我至少已经购买了有效地使用旧代码作为电子书。其中大部分是有道理的,这似乎是一本关于对旧代码进行单元测试的好书。但我仍然认为我需要一个起点,因为我们的项目 Attracs 很大。另请参阅我关于单元测试的一般问题。
该应用程序有一个 UML 模型来定义类、属性和关系,并为 Delphi 使用 Bold。在模型中的每次更改之后,我们都会进行一次往返。这会自动为文件 businessclasses.pas 和 BusinessClasses_Interface.inc 中的方法生成声明。如果更改需要更改数据库,也会生成一个 SQL 脚本。这多年来一直运作良好,但我们从未使用过任何单元测试。
所以我添加了一个新的testproject,然后依赖关系引起了麻烦。我有
[DCC 错误] Attracs_Interface_Uses.inc(10):F1026 找不到文件:“MsxSupport.dcu”
所以总结一下错误
AttracsTest.dpr 使用
BusinessClasses.pas 使用
BusinessClasses_Interface.inc 使用
Attracs_Interface_Uses.inc
那么我怎样才能打破依赖链呢?
请注意,实际上文件要大得多。模型中有 300 多个类,businessClasses.pas 有超过 53000 行代码...作为测试用例,我只有 TPerson 类和 AddResponsibility 方法。但是你应该明白其中的原理。
这是我的文件:
吸引测试.dpr
program AttracsTests;
{$IFDEF CONSOLE_TESTRUNNER}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
Forms,
TestFramework,
GUITestRunner,
TextTestRunner,
BusinessClasses in '..\..\server\code\BusinessClasses.pas',
TestBusinessClasses in 'TestBusinessClasses.pas',
ArrayOfObject in '..\..\server\code\ArrayOfObject.pas';
{$R *.RES}
begin
Application.Initialize;
if IsConsole then
TextTestRunner.RunRegisteredTests
else
GUITestRunner.RunRegisteredTests;
end.
TestBusinessClasses.pas
unit TestBusinessClasses;
interface
uses
TestFramework,
ArrayOfObject,
AttracsAttributes,
AttracsDefs,
atXMLObjModel,
BoldAttributes
BoldDBInterfaces,
BoldDefs,
BoldDeriver,
BoldDomainElement,
BoldElements,
BoldSubscription,
BoldSystem,
BoldSystemRT,
BusinessClasses, // Trigger the dependency, but also contain info about the classes get and set methods for attributes.
Classes,
Contnrs,
SysUtils,
XMLIntf,
XMLObjModel,
XMLParser;
type
TestTPerson = class(TTestCase)
strict private
FPerson: TPerson;
public
procedure SetUp; override;
procedure TearDown; override;
published
procedure TestAddResponsibility;
end;
implementation
procedure TestTPerson.SetUp;
begin
FPerson := TPerson.Create;
end;
procedure TestTPerson.TearDown;
begin
FPerson.Free;
FPerson := nil;
end;
procedure TestTPerson.TestAddResponsibility;
var
ReturnValue: Boolean;
aSession: TLogonSession;
aDevType: TDevTypeDef;
aMarketArea: TMarketArea;
begin
// TODO: Setup method call parameters
ReturnValue := FPerson.AddResponsibility(aMarketArea, aDevType, aSession);
// TODO: Validate method results
end;
initialization
// Register any test cases with the test runner
RegisterTest(TestTPerson.Suite);
end.
Attracs_Interface_Uses
AttracsDefs,
atXMLObjModel,
XMLObjModel,
XMLParser,
Contnrs,
XMLIntf,
ArrayOfObject,
BoldDBInterfaces,
MsxSupport // Line that compiler complain about
BusinessClasses_Interface.inc
(*****************************************)
(* This file is autogenerated *)
(* Any manual changes will be LOST! *)
(*****************************************)
{$IFNDEF BusinessClasses_Interface.inc}
{$DEFINE BusinessClasses_Interface.inc}
{$IFNDEF BusinessClasses_unitheader}
unit BusinessClasses;
{$ENDIF}
{$INCLUDE Attracs.inc} //PATCH
interface
uses
// interface uses
{$INCLUDE Attracs_Interface_Uses.inc} ,
// interface dependencies
// attribute classes
AttracsAttributes,
BoldAttributes,
// other
Classes,
SysUtils,
BoldDefs,
BoldSubscription,
BoldDeriver,
BoldElements,
BoldDomainElement,
BoldSystemRT,
BoldSystem;
type
{ forward declarations of all classes }
TPerson = class;
TPerson = class(TAmStateObject)
public
function AddResponsibility(aMarketArea: TMarketArea; aDevType: TDevTypeDef; aSession: TLogonSession): Boolean;
end;
function GeneratedCodeCRC: String;
implementation
uses
// implementation uses
{$INCLUDE Attracs_Implementation_Uses.inc} ,
// implementation dependencies
// other
BoldGeneratedCodeDictionary;
{$ENDIF}
商务舱.pas
(*****************************************)
(* This file is autogenerated *)
(* Any manual changes will be LOST! *)
(*****************************************)
unit BusinessClasses;
{$DEFINE BusinessClasses_unitheader}
{$INCLUDE BusinessClasses_Interface.inc}
{ Includefile for methodimplementations
Have concrete implementation of methods}
{$INCLUDE Person.inc}
// Some get and set methods fopr attributes in the class
// attribute FirstName
function TPerson._Get_M_FirstName: TBAString;
begin
assert(ValidateMember('TPerson', 'FirstName', 14, TBAString));
Result := TBAString(BoldMembers[14]);
end;
function TPerson._GetFirstName: String;
begin
Result := M_FirstName.AsString;
end;
procedure TPerson._SetFirstName(const NewValue: String);
begin
M_FirstName.AsString := NewValue;
end;
procedure InstallBusinessClasses(BoldObjectClasses: TBoldGeneratedClassList);
begin
BoldObjectClasses.AddObjectEntry('Person', TPerson);
end;
var
CodeDescriptor: TBoldGeneratedCodeDescriptor;
initialization
CodeDescriptor := GeneratedCodes.AddGeneratedCodeDescriptorWithFunc('BusinessClasses', InstallBusinessClasses, InstallObjectListClasses, GeneratedCodeCRC);
finalization
GeneratedCodes.Remove(CodeDescriptor);
end.
个人公司
function TPerson.AddResponsibility(aMarketArea: TMarketArea; aDevType: TDevTypeDef; aSession: TLogonSession): Boolean;
var
vOCL: String;
vDevResponse: TDevResponsible;
begin
vOCL := Format('DevResponsible.allinstances->select((devType.TypeName = ''%s'') and (marketArea.name = ''%s''))->first',
[aDevType.TypeName, aMarketArea.name]);
vDevResponse := GetApplicationKernel.EvaluateExpressionAsDirectElement(vOCL) as TDevResponsible;
if not Assigned(vDevResponse) then
vDevResponse := GetApplicationKernel.CreateAMObject('DevResponsible') as TDevResponsible;
if Assigned(vDevResponse) then
begin
vDevResponse.marketArea := aMarketArea;
vDevResponse.devType := aDevType;
vDevResponse.responsiblePers := self;
NotifyModificationHistory(Now, aSession, Format('Responsible for %s marketarea: %s', [aDevType.TypeName, aMarketArea.Name]));
Result := True;
end
else
Result := False;
end;