別のプロジェクトで非公開プロパティをテストする必要がある場合は、Microsoft単体テストウィザードを使用してAccessorオブジェクトを作成します。単体テスト内でヘルパー関数を作成して、すべての単体テストメソッドで同じコードを繰り返さないようにします。現在、1つは標準オブジェクトを取得し、もう1つはAccessorバージョンを取得することを除いて、ほぼ同じ2つのテストがあります。アクセサーは標準バージョンに基づいているので、私は1つの機能を持つことができるはずであり、ジェネリックを使用して達成できるはずだと思います。問題は、失敗を再入力してコンパイルしようとしています。
既存の2つの関数は次のとおりです。
// Common function to create a new test record with standard Account object
internal static void CreateAccount(out Account account, bool saveToDatabase)
{
DateTime created = DateTime.Now;
string createdBy = _testUserName;
account = new Account(created, createdBy);
account.Notes = Utilities.RandomString(1000);
if (saveToDatabase)
account.Create();
}
// Common function to create a new test record with Account_Accessor
internal static void CreateAccount(out Account_Accessor account, bool saveToDatabase)
{
DateTime created = DateTime.Now;
string createdBy = _testUserName;
account = new Account_Accessor(created, createdBy);
account.Notes = Utilities.RandomString(1000);
if (saveToDatabase)
account.Create();
}
組み合わせた関数の署名を次のように変更してみました。
internal static void CreateAccount<T>(out T account, bool saveToDatabase) {...}
しかし、TをAccountまたはAccount_Accessorに正しくリキャストできませんでした。助言がありますか?