すぐに使える機能 (MSTest.exe だと思います) と Microsoft Fakes (スタブと Shim) を使用して、単体テストについてもう少し学習しようとしています。
Visual Studio 2012 Ultimate と .Net 4.5 Framework を使用しています。
単一の出力値を返すストアド プロシージャ (SQL Server) を呼び出す次のコードがあるとします (簡単にするため)。
public string GetSomeDatabaseValue()
{
string someValue = String.Empty;
SqlParameter paramater = new SqlParameter();
paramater.ParameterName = "@SomeParameter";
paramater.Direction = ParameterDirection.Output;
paramater.SqlDbType = SqlDbType.NVarChar;
paramater.Size = 50;
try
{
using(SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString))
{
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = "SomeStoredProcedure";
command.Parameters.Add(paramater);
connection.Open();
command.ExecuteNonQuery();
if (command.Parameters["@SomeParameter"] != null)
{
someValue= Convert.ToString(command.Parameters["@SomeParameter"].Value);
}
}
}
}
catch(SqlException)
{
throw;
}
return someValue;
}
- 出力値を特定の値に設定できるように、シムやスタブを使用してテストできますか?
- もしそうなら、どのように?
- これには単体テストを使用する必要がありますか?
私はこのチュートリアルに従い、それを理解して曜日に適応させることができました.
MSの従業員がデータベースを分離してテストできるようにコメントしているように、VS2012データベースユニットテスト機能が2012年末までに利用可能になる(または復活する)のを待っています。