利用COM+对数据库操作进行单元测试

当单元测试需要对数据库执行CRUD(Create,Retrieve,Update,Delete)操作时,测试过后会在我们的数据库中留下大量重复的垃圾数据,这些垃圾很碍眼不是吗?而且我们的下一个测试有可能因为这些垃圾产生一些错误。

那么我们要如何处理这些垃圾数据和保证测试的稳定的呢?显然,我们需要在每次测试之前和测试完成之后让数据库都保持相同的状态。换句话说,就是我们需要"undo"这些在测试中对数据库执行的CRUD操作。

对于我们需要的这种"undo"操作,你以前是怎么做的呢?手动的移除还是使用ADO.NET的事物处理呢?这些方法都可行,但对我们来说还不够好。因为它们都需要我们编写更多的代码,影响我们的开发效率。

现在,就要开始说本文的重点了,利用COM+的自动事务处理功能来帮助我们实现我们的"undo"。

首先,写一个基类,此类能够在每一个方法完成后自动回滚对数据库的操作:
None.gif using  System;
None.gif
using  NUnit.Framework;
None.gif
using  System.EnterpriseServices;
None.gif
None.gif
namespace  TransactionTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [TestFixture]
InBlock.gif    [Transaction(TransactionOption.Required)]
InBlock.gif    
public class DatabaseFixture:ServicedComponent
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [TearDown]
InBlock.gif        
public void TransactionTearDown()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(ContextUtil.IsInTransaction)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ContextUtil.SetAbort();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

下面再写一个使用此基类的示例程序:
None.gif using  System;
None.gif
using  NUnit.Framework;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
None.gif
namespace  TransactionTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class CategoryTests:DatabaseFixture
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string CONN = @"Server=192.168.0.58\sun;Database=Northwind;uid=帐号;pwd=密码";
InBlock.gif
InBlock.gif        [Test]
InBlock.gif        
public void InsertTest()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string categoryName = Insert("Test category");
InBlock.gif            VerifyRowExists(categoryName, 
true);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Test]
InBlock.gif        
public void DeleteTest()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string categoryName = Insert("Test category");
InBlock.gif            Delete(categoryName);
InBlock.gif            VerifyRowExists(categoryName, 
false);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 新增一条记录
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="categoryName"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private string Insert(string categoryName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (SqlConnection conn = new SqlConnection(CONN))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strSQL = "Insert Categories (CategoryName) values('" + categoryName + "')";
InBlock.gif                SqlCommand cmd 
= new SqlCommand(strSQL, conn);
InBlock.gif                conn.Open();
InBlock.gif                cmd.ExecuteNonQuery();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return categoryName;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 删除一条记录
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="categoryName"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private string Delete(string categoryName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (SqlConnection conn = new SqlConnection(CONN))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strSQL = "Delete from Categories Where CategoryName='" + categoryName + "'";
InBlock.gif                SqlCommand cmd 
= new SqlCommand(strSQL, conn);
InBlock.gif                conn.Open();
InBlock.gif                cmd.ExecuteNonQuery();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return categoryName;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 对执行的数据库操作进行验证
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="categoryName"></param>
ExpandedSubBlockEnd.gif        
/// <param name="shouldExist"></param>

InBlock.gif        private void VerifyRowExists(string categoryName,bool shouldExist)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlConnection conn 
= new SqlConnection(CONN);
InBlock.gif            conn.Open();
InBlock.gif
InBlock.gif            
string strSQL = "Select * from Categories where CategoryName='" + categoryName + "'";
InBlock.gif            SqlCommand cmd 
= new SqlCommand(strSQL, conn);
InBlock.gif            SqlDataReader dr 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
InBlock.gif            Assert.AreEqual(shouldExist,dr.HasRows);
InBlock.gif            dr.Close();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


如果编译上面的程序,还需要给程序加一个强名称,否则是无法通过的

 

在命令行使用 sn -k test.snk
然后把 test.snk 拷贝到程序目录中,再设置
[assembly: AssemblyKeyFile(@"..\..\..\test.snk")]





原文: http://weblogs.asp.net/rosherove/articles/dbunittesting.aspx

转载于:https://www.cnblogs.com/netflu/archive/2005/07/29/202805.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值