0

我想设计一个抽象类来对我的应用程序中的数据库相关功能进行单元测试。如果我调用 CreateDatabase 或 ConnectToExistingDatabase 我想将数据库信息存储在我班级的私有部分

type
  TTestDatabase = class
    FDatabaseConnection: TADOConnection;
  private
    FServerName: string;
    FDatabaseName: string;
    FPathtoSqlScripts: string;
  public
    procedure CreateDatabase(AServerName, ADatabaseName,
      APathtoSqlScripts: string);
    procedure ConnectToExistingDatabase(AServerName, ADatabaseName: string);
    function DropDatabase: Boolean;
  end; 

procedure TTestDatabase.ConnectToExistingDatabase(AServerName,
  ADatabaseName: string);
begin
  FServerName := AServerName;
  FDatabaseName := ADatabaseName;
  ConnecttoDatabase(FServerName, FDatabaseName, FDatabaseConnection);
end;

constructor TTestDatabase.Create;
begin
  FDatabaseConnection := TADOConnection.Create(nil);
end;

procedure TTestDatabase.CreateDatabase(AServerName, ADatabaseName,
  APathtoSqlScripts: string);
var
  aSql: string;
begin
  FServerName := AServerName;
  FDatabaseName := ADatabaseName;
  FPathtoSqlScripts := APathtoSqlScripts;
  ADO_CreateDatabase(FDatabaseName, FServerName);  
end;

function TTestDatabase.DropDatabase: Boolean;
var
  aSql: string;
begin
  // connect new to server
  FServerName := MYSERVER;
  FDatabaseConnection.Connected := False;
  ConnecttoDatabase(FServerName, 'master', FDatabaseConnection);
  // drop the database
  FDatabaseName := 'TEST_DATABASE';

  if (FDatabaseName <> '') then
  begin
    FDatabaseConnection.Execute('use master');
    aSql := ' drop database ' + FDatabaseName;
    FDatabaseConnection.Execute(aSql);
  end
  else
    Result := False;
end;

问题:如果要运行 dropdatabase 函数,值 FDatabasename .... 始终是空字符串。如果我重新定义例如。FServername(如代码示例所示)问题已解决,但这是一个不可接受的解决方案....

执行单元测试功能时,我不能将值存储在类的私有部分吗????

这是单元测试代码,尚未完成。希望它会显示问题

procedure TestTTestDatabase.TestCreateDatabase;
var
  aServerName: string;
  aDatabaseName: string;
  aPathtoSqlScripts: string;
begin
  // TODO: Setup method call parameters
  aServerName := MYSERVER;
  aDatabaseName := 'TEST_UNITTEST';
  aPathtoSqlScripts  := 'C:\testpath\';

  FTestDatabase.CreateDatabase(aServerName, aDatabaseName, aPathtoSqlScripts);
  // TODO: Validate method results
end;

procedure TestTTestDatabase.TestConnectToExistingDatabase;
var
  aServerName: string;
  aDatabaseName: string;
begin
  // TODO: Setup method call parameters
  aServerName := MYSERVER;
  aDatabaseName := 'TEST_UNITTEST';

  try
    FTestDatabase.ConnectToExistingDatabase(aServerName, aDatabaseName);
  except
    fail('connection to exception fails');
  end;
  // TODO: Validate method results
  CheckTrue(FTestDatabase.Connection);
end;

procedure TestTTestDatabase.TestDropDatabase;
var
  ReturnValue: Boolean;
begin
  ReturnValue := FTestDatabase.DropDatabase;
  // TODO: Validate method results
end;
4

0 回答 0