Class SqlServerTestHelper
Helper class for unit testing using real SQL Server database.
Inheritance
Namespace: Kros.UnitTests
Assembly: Kros.Utils.dll
Syntax
public class SqlServerTestHelper : IDisposable
Remarks
In general, the unit tests should not require real database. But in some cases, this is necessary. This class manages creation of temporary database, which the tests will use. Database name is generated to be unique and after finishing (Dispose()), the database is deleted. Connection to created database is available in Connection property.
Examples
// V connection string-u nie je určená databáza, pretože tá sa automaticky vytvorí s náhodným
// menom. Na konci práce sa databáza automaticky vymaže.
private const string BaseConnectionString = "Data Source=SQLSERVER;Integrated Security=True;";
private const string CreateTestTableScript =
@"CREATE TABLE [dbo].[TestTable] (
[Id] [int] NOT NULL,
[Name] [nvarchar](255) NULL,
CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED ([Id] ASC) ON [PRIMARY]
) ON [PRIMARY];";
[Fact]
public void DoSomeTestWithDatabase()
{
using (var serverHelper = new SqlServerTestHelper(BaseConnectionString, "TestDatabase", CreateTestTableScript))
{
// Do tests with connection serverHelper.Connection.
}
}
Constructors
SqlServerTestHelper(String, String)
Creates an instance of helper with connection baseConnectionString
and base database name
baseDatabaseName
.
Declaration
public SqlServerTestHelper(string baseConnectionString, string baseDatabaseName)
Parameters
Type | Name | Description |
---|---|---|
System.String | baseConnectionString | Base connection string to SQL Server, where database will be created. |
System.String | baseDatabaseName | Base database name. GUID will be appended to it. The value is not required. |
SqlServerTestHelper(String, String, IEnumerable<String>)
Creates an instance of helper with connection baseConnectionString
and base database name
baseDatabaseName
. Created database will be initialized with scripts from
initDatabaseScripts
.
Declaration
public SqlServerTestHelper(string baseConnectionString, string baseDatabaseName, IEnumerable<string> initDatabaseScripts)
Parameters
Type | Name | Description |
---|---|---|
System.String | baseConnectionString | Base connection string to SQL Server, where database will be created. |
System.String | baseDatabaseName | Base database name. GUID will be appended to it. The value is not required. |
System.Collections.Generic.IEnumerable<System.String> | initDatabaseScripts | List of scripts, which are executed when database is created. For example, they can be scripts to create necessary tables and data. |
SqlServerTestHelper(String, String, String)
Creates an instance of helper with connection baseConnectionString
and base database name
baseDatabaseName
. Created database will be initialized with script
initDatabaseScript
.
Declaration
public SqlServerTestHelper(string baseConnectionString, string baseDatabaseName, string initDatabaseScript)
Parameters
Type | Name | Description |
---|---|---|
System.String | baseConnectionString | Base connection string to SQL Server, where database will be created. |
System.String | baseDatabaseName | Base database name. GUID will be appended to it. The value is not required. |
System.String | initDatabaseScript | The script, which is executed when database is created. For example, it can be script to create some table. |
Properties
BaseConnectionString
Base connection string to SQL Server, where temporary database will be created. Database name does not need to be set in connection stirng, because it will be generated.
Declaration
public string BaseConnectionString { get; }
Property Value
Type | Description |
---|---|
System.String |
BaseDatabaseName
Base database name. GUID is appended to this name, to make database name unique. If BaseDatabaseName
is empty, the database name will be just that GUID.
Declaration
public string BaseDatabaseName { get; }
Property Value
Type | Description |
---|---|
System.String |
Connection
Connection to created database.
Declaration
public SqlConnection Connection { get; }
Property Value
Type | Description |
---|---|
System.Data.SqlClient.SqlConnection |
Methods
Dispose()
Declaration
public void Dispose()
Dispose(Boolean)
Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | disposing |
GenerateDatabaseName()
Generates a name for database, which is created on SQL Server. Returned name is composed from BaseDatabaseName (if specified) and generated GUID, to make it unique.
Declaration
protected virtual string GenerateDatabaseName()
Returns
Type | Description |
---|---|
System.String | Database name. |
InitDatabase()
Initializes a database. Method is executed once after creating the database and it executes scripts which were specified in constructor.
Declaration
protected virtual void InitDatabase()