Show / Hide Table of Contents

    Class SqlServerDatabaseTestBase

    Base class for database integration tests on Microsoft SQL Server. The class takes care of creating and initialization of database. Inherited classes just use connection to this database.

    Inheritance
    System.Object
    SqlServerDatabaseTestBase
    Namespace: Kros.UnitTests
    Assembly: Kros.Utils.dll
    Syntax
    public abstract class SqlServerDatabaseTestBase : IDisposable
    Remarks

    Database with unique name is created at the begining and it is deleted when Dispose() is called. The created database may be initielized with own scripts in DatabaseInitScripts.

    Descendant classes must override BaseConnectionString to set the connection to SQL Server.

    public class SomeDatabaseTests
        : Kros.UnitTests.SqlServerDatabaseTestBase
    {
        protected override string BaseConnectionString => "Data Source=TESTSQLSERVER;Integrated Security=True";
    
        [Fact]
        public void Test1()
        {
            using (var cmd = ServerHelper.Connection.CreateCommand())
            {
                // Use cmd to execute queries.
            }
        }
    
        [Fact]
        public void Test2()
        {
        }
    }

    Constructors

    SqlServerDatabaseTestBase()

    Creates an instance of SqlServerDatabaseTestBase.

    Declaration
    public SqlServerDatabaseTestBase()

    Properties

    BaseConnectionString

    Base connection string to SQL Server, where database will be created. It does not need to have database name, because it will be generated to make it unique.

    Declaration
    protected abstract string BaseConnectionString { get; }
    Property Value
    Type Description
    System.String
    See Also
    SqlServerTestHelper
    BaseConnectionString

    BaseDatabaseName

    Base database name. GUID is appended to this name to make it unique. Default implementation returns class full name (GetType().FullName) with underscore (_) appended.

    Declaration
    protected virtual string BaseDatabaseName { get; }
    Property Value
    Type Description
    System.String
    See Also
    BaseDatabaseName
    SqlServerTestHelper

    DatabaseInitScripts

    SQL scripts for initializing created database.

    Declaration
    protected virtual IEnumerable<string> DatabaseInitScripts { get; }
    Property Value
    Type Description
    System.Collections.Generic.IEnumerable<System.String>
    Remarks

    The class creates a database for tests using SqlServerTestHelper. If it is necessary to have this database initialized (tables, data...), this is the list of scripts for it.

    ServerHelper

    Helper for accessing database using its Connection property.

    Declaration
    protected SqlServerTestHelper ServerHelper { get; }
    Property Value
    Type Description
    SqlServerTestHelper

    Methods

    Dispose()

    Declaration
    public void Dispose()

    Dispose(Boolean)

    Declaration
    protected virtual void Dispose(bool disposing)
    Parameters
    Type Name Description
    System.Boolean disposing

    CheckDisposed()

    Checks, if the instance was disposed of (Dispose() was called). If yes, it throws System.ObjectDisposedException.

    Declaration
    protected void CheckDisposed()
    Exceptions
    Type Condition
    System.ObjectDisposedException

    If the instance was already disposed of.

    Back to top KROS a.s.