CreateDatabaseConnection 方法的单元测试
使用:与 VS2010 集成的 Microsoft 单元测试框架
我有一个实现此接口的类。
public interface IConnectionManager
{
IDbConnection OpenDatabase(string path);
void CloseDatabase();
}
我想为这些方法创建一组测试,但不太确定如何继续。
测试这个的最好方法是什么?
谢谢。
编辑:
我的 OpenDatabase 实现看起来像这样:
public OleDbConnection OpenDatabase (string p_path)
{
if (Library.StringOperations.IsNullOrEmpty (p_path))
return null;
bool error = false;
string connectionString= @"CONNECTION STRING HERE";
try
{
OleDbConnection con= new OleDbConnection (connectionString);
con.Open ();
}
catch (Exception)
{
error = true;
}
if (!error)
return con;
return null;
}
正如 @rdkleine 所建议的,我想测试返回的连接。
几个问题:
我是否需要创建一个新的 OleDbConnection 对象来与我返回的对象进行比较,或者我应该检查我返回的连接对象属性吗?
这种情况下可以使用模拟对象吗?
考虑到我正在测试与数据库的连接,这仍然是单元测试还是集成测试?
再次感谢。
Using: Microsoft Unit Testing Framework integrated with VS2010
I have a class that implements this interface.
public interface IConnectionManager
{
IDbConnection OpenDatabase(string path);
void CloseDatabase();
}
I would like to create a set of Tests for these methods but not quite sure how to proceed.
What is the best way to test this?
Thanks.
EDIT:
My OpenDatabase
implementation looks something like this:
public OleDbConnection OpenDatabase (string p_path)
{
if (Library.StringOperations.IsNullOrEmpty (p_path))
return null;
bool error = false;
string connectionString= @"CONNECTION STRING HERE";
try
{
OleDbConnection con= new OleDbConnection (connectionString);
con.Open ();
}
catch (Exception)
{
error = true;
}
if (!error)
return con;
return null;
}
As suggested by @rdkleine I want to test the returned connection.
A few questions:
Do I need to create a new OleDbConnection object to compare with my returned object or should I check my returned connection object proprieties?
Can Mock Objects be used in this case?
Having in mind that I am testing a connection to a DB is this still Unit Testing or Integration Testing?
Thanks again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从简单开始。
等
阅读这本关于 TDD 的书:)
Start simple.
etc
Read this book about TDD :)