You are here: Code Zone » Unit Testing
 Register    Login 
Code Zone Minimize  

Free Source Code Minimize 

Here you will find .Net source code, for ASP.Net, WinForms, WCF, Unit Testing, Security and more...

If you find this code useful, please visit our sponsers who help keep this site running.

Tip: Turn off line numbers when using Copy and Paste.


Related Links Minimize  

Unit Testing Code Minimize  
TransactionalUnitTestBase.NUnit.cs
TransactionalUnitTestBase.VS.cs
1using System;
2using System.Collections;
3using System.IO;
4using System.Transactions;
5using System.Xml.Serialization;
6using Microsoft.VisualStudio.TestTools.UnitTesting;
7
8namespace UnitTest
9{
10    /// <!-- 
11    ///     Code Courtesy Of http://www.purecoding.net
12    ///     Permissions : Free for general use
13    ///     Keywords: Diagnostics, Unit Testing, c#, .net, Windows, aspnet. 
14    ///     Filename: TransactionalUnitTestBase.cs
15    /// -->
16    /// <summary>
17    /// This class provides Transactional ability to unit tests.  Similar to the com+ approach, all code
18    /// executed within the scope of this class will rollback because no commit is called.
19    /// The TearDown function will Dispose the transaction to avoid locking issues with other tests.
20    /// If you have a Transaction import issue while debugging, increase the timeout.
21    /// </summary>
22    /// <remarks>
23    ///     This class will only work with .Net 2.0 due to the use of <see cref="TransactionScope"/> 
24    /// </remarks>
25    public abstract class TransactionalUnitTestBase
26    {
27        private TimeSpan _TransactionTimeout = new TimeSpan(0,0,10,0); 
28        //Timeout Set to 10 Minutes for debugging reasons
29        private TransactionScope _Transaction; 
30        protected Hashtable _ForeignEntities = new Hashtable();
31        
32        public TransactionalUnitTestBase()
33        {
34            _Transaction = new TransactionScope(TransactionScopeOption.RequiresNew,
35                _TransactionTimeout); 
36            //Stops any database commits - Requires reference to System.Transactions - .Net 2 Only
37        }
38        
39        /// <summary>
40        /// This function serialises both objects and compares the string value.
41        /// This avoids having to implement Equals on the entities.
42        /// </summary>
43        /// <param name="first"></param>
44        /// <param name="second"></param>
45        /// <returns></returns>
46        protected virtual bool AreSameValues(object first, object second)
47        {
48            string test1 = GetObjectString(first);
49            string test2 = GetObjectString(second);
50            return test1.Equals(test2);
51        }
52        protected virtual string GetObjectString(object toSerialise)
53        {
54            XmlSerializer serializer = new XmlSerializer(toSerialise.GetType());
55            MemoryStream ms = new MemoryStream();
56            StreamWriter sw = new StreamWriter(ms, System.Text.Encoding.UTF8);
57            StreamReader sr = new StreamReader(ms, System.Text.Encoding.UTF8);
58            try
59            {
60                serializer.Serialize(sw, toSerialise);
61                sr.BaseStream.Position = 0;
62                return sr.ReadToEnd();
63            }
64            finally
65            {
66                sr.Close();
67                sw.Close();
68                ms.Close();
69            }
70        }
71
72       
73        [ClassCleanup]
74        public virtual void UnitTestTearDown()
75        {
76            _Transaction.Dispose();
77        }
78    }
79}
80

 Copyright 2008 PureCoding.net
 Terms Of Use     Privacy Statement