我如何编写与数据库对话的单元集成测试。例如:
public int GetAppLockCount(DbConnection connection)
{
string query :=
"SELECT"+CRLF+
" tl.resource_type AS ResourceType,"+CRLF+
" tl.resource_description AS ResourceName,"+CRLF+
" tl.request_session_id AS spid"+CRLF+
"FROM sys.dm_tran_locks tl"+CRLF+
"WHERE tl.resource_type = 'APPLICATION'"+CRLF+
"AND tl.resource_database_id = ("+CRLF+
" SELECT dbid"+CRLF+
" FROM master.dbo.sysprocesses"+CRLF+
" WHERE spid = @@spid)";
IRecordset rdr = Connection.Execute(query);
int nCount = 0;
while not rdr.EOF do
{
nCount := nCount+1;
rdr.Next;
}
return nCount;
}
在这种情况下,我试图消除这个错误代码(IRecordset
返回空记录集)。
[UnitTest]
void TestGetLockCountShouldAlwaysSucceed();
{
DbConnection conn = GetConnectionForUnit_IMean_IntegrationTest();
GetAppLockCount(conn);
CheckTrue(True, "We should reach here, whether there are app locks or not");
}
现在我只需要一种在运行单元集成测试时连接到某个数据库的方法。
人们是否将连接字符串存储在某个地方以供测试运行者查找?.ini
或.xml
或文件.config
?
注意:语言/框架不可知。该代码有意包含以下元素:
- C#
- 德尔福
- ADO.net
- ADO
- 单元
- 单位
为了把那个点带回家。