Database Testclass在单元测试中显示奇怪的行为

时间:2013-09-25 13:08:48

标签: delphi

我想在我的应用程序中设计一个抽象类来对数据库相关函数进行异常单元测试。 如果我调用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;

0 个答案:

没有答案