chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
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(
asked by swtrse (4k points)

2 Answers

0 votes
It seems I'm still missing a few classes in order to reproduce this issue - let's take it offline

Dror Helper
Typemock Support
answered by dhelper (11.9k points)
0 votes
I found the solution myself writing a few other unit test.

Seems like I missed the fakt that when using Isolate.WhenCalled on void-Methods I have to use IgnoreCall().
answered by swtrse (4k points)
...