2
這裏看似簡單的一個。我如何使用單元測試來測試這個代碼塊的異常處理?單元測試異常處理
public DbFactoryResponseType Close()
{
DbFactoryResponseType dbFactoryResponse = new DbFactoryResponseType();
try
{
if (m_isConnected)
{
m_isConnected = false;
if (m_hasRecordSet)
{
m_hasRecordSet = false;
m_dbFactoryDatabaseDataReader.Close();
m_dbFactoryDatabaseDataReader.Dispose();
}
m_dbFactoryDatabaseCommand.Dispose();
m_dbFactoryDatabaseConnection.Close();
m_dbFactoryDatabaseConnection.Dispose();
}
dbFactoryResponse.ExceptionMessage = "";
dbFactoryResponse.Success = true;
dbFactoryResponse.UserFriendlyMessage = "OK";
return dbFactoryResponse;
}
catch (Exception ex)
{
dbFactoryResponse.ExceptionMessage = ex.Message;
dbFactoryResponse.Success = false;
dbFactoryResponse.UserFriendlyMessage = "Error: Error while attempting to close the database connection.";
return dbFactoryResponse;
}
}
這裏是我到目前爲止,但我不知道如何使異常火讓我測試輸出。
/// <summary>
/// Test method to test closing a PosgreSQL database connection.
/// </summary>
[TestMethod]
public void TestClosePostgreSQLConnectionException()
{
const string connectionString = "Server=myIp;Port=myPort;Database=myDatabase;User Id=myUser;Password=myPassword;";
const string provider = "Npgsql";
DbProviderFactoryConnection aDbProviderFactoryConnection = new DbProviderFactoryConnection(connectionString, provider);
DbFactoryResponseType dbFactoryResponseType = aDbProviderFactoryConnection.Close();
Assert.IsNotNull(dbFactoryResponseType);
Assert.AreEqual(false, dbFactoryResponseType.Success);
}
你有任何鏈接或例子,我將如何做到這一點? – CSharpened
哦,是的,我將很快發佈一個 –
優秀。謝謝 – CSharpened