如何编写异常处理的自动化测试
我有很多以下类型的代码(每个代码都从不同的表获取数据)。
public class TableOneDAO
{
public TableOneDAO(IDbConnection dbConnection)
{
_dbConnection = dbConnection;
_log = LogFacade.GetLog();
_tableOneRowMapper = new TableOneRowMapper();
}
public IList<TableOneRecord> FindAll()
{
try
{
using (IDbCommand cmd = _dbConnection.CreateCommand())
{
cmd.CommandText = TableOneSql.FindAllSql();
return _tableOneRowMapper.GetObjectList(cmd.ExecuteReader());
}
}
catch (Exception exception)
{
_log.Error(exception.Message, exception);
throw;
}
}
private readonly IDbConnection _dbConnection;
private readonly TableOneRowMapper_tableOneRowMapper;
private readonly ILog _log;
}
_tableOneRowMapper.GetObjectList(cmd.ExecuteReader()) 从数据库获取数据并将其映射到列表。我们过去遇到过导致映射出现问题的记录问题,现在希望立即记录这些问题,然后将它们发送到调用应用程序。除了异常处理之外,我对所有内容都进行了测试。我必须保留通用异常处理。
我正在使用或有以下可供使用:
- .Net 3.5
- VS 2008
- NUnit 2.5.10.11092
- Rhino Mocks 3.6
我的问题:
- 我如何为 异常处理?
- 我需要注入 TableOneRowMapper 和 模拟它,以便在调用时抛出异常?
- 还有其他的吗 有什么方法可以在不注入的情况下做到这一点?
- 我不担心测试异常处理吗?
I have a lot of the following type of code (each gets data from different tables).
public class TableOneDAO
{
public TableOneDAO(IDbConnection dbConnection)
{
_dbConnection = dbConnection;
_log = LogFacade.GetLog();
_tableOneRowMapper = new TableOneRowMapper();
}
public IList<TableOneRecord> FindAll()
{
try
{
using (IDbCommand cmd = _dbConnection.CreateCommand())
{
cmd.CommandText = TableOneSql.FindAllSql();
return _tableOneRowMapper.GetObjectList(cmd.ExecuteReader());
}
}
catch (Exception exception)
{
_log.Error(exception.Message, exception);
throw;
}
}
private readonly IDbConnection _dbConnection;
private readonly TableOneRowMapper_tableOneRowMapper;
private readonly ILog _log;
}
_tableOneRowMapper.GetObjectList(cmd.ExecuteReader()) gets the the data from the database and maps it to a List. We have had past issues of records causing issues in the mapping and now want to log those right away and then trow them to the calling application. I have tests for everything but the exception handling. I must keep the generic exception handling in place.
I am using or have the following available to use:
- .Net 3.5
- VS 2008
- NUnit 2.5.10.11092
- Rhino Mocks 3.6
MY QUESTIONS:
- How can I create automated tests (unit or integration) for the
exception handling? - Do I need to inject the TableOneRowMapper and
mock it so it throws an exception when called? - Are there any other
ways to do this without injecting it? - Do I not worry about testing the exception handling?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,否则这将是集成测试而不是单元测试
???
No, otherwise it would be an integration test rather unit test
???
•如何为异常处理创建自动化测试(单元或集成)?
NUnit 有一个 ExpectedException 属性 (http://www.nunit.org/index.php?p=exception&r=2.4),用于验证是否已引发异常。
您还需要验证是否已记录异常。 LogFacade.GetLog() 可以返回模拟日志吗?
•有没有其他方法可以在不注射的情况下做到这一点?
这取决于您对“注入”的定义。
在注入的一般意义上 - 使用类中未创建的内容 - 您确实需要注入映射器,否则,正如@sll所说,它不是单元测试。
如果您担心更改 TableOneDAO 的构造函数,您可以使用工厂来创建映射器,而不是在 DAO 中创建它。在您的测试中,您可以向工厂注册模拟映射器。
其他
如果您要经常这样做(创建和测试大量表 DAO),我将创建一个基本 TableDAO 类并从中派生所有其他类。派生类只需要提供mapper和sql字符串。
这样做,异常处理只需要进行一次单元测试;为基类。
嗯,
艾伦。
•How can I create automated tests (unit or integration) for the exception handling?
NUnit has an ExpectedException attribute (http://www.nunit.org/index.php?p=exception&r=2.4) which verifies that an exception has been thrown.
You will also want to verify that the exception has been logged. Can LogFacade.GetLog() return a mock log?
•Are there any other ways to do this without injecting it?
It depends upon your definition of 'injecting'.
In the general sense of injecting - using something not created in the class - you do need to inject the mapper, otherwise, as @sll says, it's not a unit test.
If you are worried about changing the ctor for TableOneDAO, you can use a factory to create the mapper instead of creating it within the DAO. In your tests you can register the mock mapper with the factory.
Other
If you are going to be doing this a lot (creating and testing lots of table DAOs) I would create a base TableDAO class and derive all the others from it. The derived classes only need to provide the mapper and sql string.
Doing it this way, the exception handling only needs to be unit tested once; for the base class.
hth,
Alan.