I have this little helper-class, that I want to UnitTest.
namespace JustTryIt
{
using System;
using System.Data.SqlClient;
using System.Threading;
/// <summary>
/// Ändert einen Codeblock in SqlConnection gebunden. Diese Klasse kann nicht geerbt werden.
/// </summary>
public sealed class SqlConnectionScope : IDisposable
{
#region Member
private bool _disposed;
private SqlConnection _expectedCurrent;
private SqlConnection _savedCurrent;
private SqlConnectionScope _savedCurrentScope;
private Thread _scopeThread;
private ContextData _threadContextData = ContextData.CurrentData;
#endregion
#region Eigenschaften
#endregion
#region Methoden
/// <summary>
/// Gibt einem <see>-Objekt Gelegenheit zu dem Versuch, Ressourcen freizugeben und andere Bereinigungen durchzuführen, bevor das Objekt von der Garbage Collection freigegeben wird.
/// </summary>
~SqlConnectionScope()
{
Dispose(false);
}
/// <summary>
/// Initialisiert eine neue Instanz der <see>-Klasse.
/// </summary>
/// <param>
/// Der Connection-String für die neue Connection.
/// </param>
public SqlConnectionScope(string connectionString)
{
_scopeThread = Thread.CurrentThread;
_savedCurrent = _threadContextData.CurrentSqlConnection;
_savedCurrentScope = _threadContextData.CurrentSqlConnectionScope;
_expectedCurrent = _savedCurrent;
if (_expectedCurrent == null || _expectedCurrent.ConnectionString != connectionString)
{
_expectedCurrent = new SqlConnection(connectionString);
}
PushScope();
}
/// <summary>
/// Beendet den Userbereich.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!_disposed)
{
if (_scopeThread != Thread.CurrentThread)
{
throw new InvalidOperationException(Resource.InvalidScopeThread);
}
if (_threadContextData.CurrentSqlConnectionScope != this)
{
throw new InvalidOperationException(Resource.InvalidScope);
}
_disposed = true;
PopScope();
if (disposing)
{
//Alle verwalteten Ressourcen freigeben
}
//Alle nicht verwalteten Ressourcen freigeben.
}
}
private void PopScope()
{
if (_savedCurrent == null || _expectedCurrent.ConnectionString != _savedCurrent.ConnectionString)
{
_expectedCurrent.Close();
}
_threadContextData.CurrentSqlConnectionScope = _savedCurrentScope;
_threadContextData.CurrentSqlConnection = _savedCurrent;
}
private void PushScope()
{
if (_savedCurrent == null || _expectedCurrent.ConnectionString != _savedCurrent.ConnectionString)
{
_expectedCurrent.Open();
}
_threadContextData.CurrentSqlConnection = _expectedCurrent;
_threadContextData.CurrentSqlConnectionScope = this;
}
#endregion
}
}
I'am working with VS2008 and created an UnitTestclass like this
namespace JustTryIt.Tests
{
using System;
using System.Data.SqlClient;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TypeMock.ArrangeActAssert;
using JustTryIt;
[TestClass]
[DeploymentItem("JustTryIt.dll")]
[DeploymentItem("de/JustTryIt.resources.dll", "de")]
public class SqlConnectionScopeTest
{
private TestContext testContextInstance;
private System.Globalization.CultureInfo _currentCulture;
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
[TestInitialize]
public void MyTestInitialize(