次のコードのテストに助けが必要です
public virtual void Update(T entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
int iretries = 0;
bool success = false;
do
{
try
{
this.context.SaveChanges();
success = true;
}
catch (DbUpdateConcurrencyException ex)
{
// Get the current entity values and the values in the database
// as instances of the entity type
var entry = ex.Entries.Single();
var databaseValues = entry.GetDatabaseValues();
// Choose an initial set of resolved values. In this case we
// make the default be the values currently in the database: StoreWins
object resolvedValues = ResolveConcurrency(databaseValues.ToObject());
// Update the original values with the database values and
// the current values with whatever the user choose.
entry.OriginalValues.SetValues(databaseValues);
entry.CurrentValues.SetValues(resolvedValues);
// give up after n retries
if (++iretries == NUMBER_OF_CONC_RETRIES)
throw;
}
catch (Exception)
{
//rethrow
throw;
}
} while (!success);
}
DbUpdateConcurrencyException
ブランチを単体テストしたい。
したがって、1 つの単純なテスト シナリオは次のようになります。
- 新規作成
DbUpdateConcurrencyException
SaveChanges
上記の例外をスローするモックSaveChanges
の番号が呼び出されたことを確認します。NUMBER_OF_CONC_RETRIES
Update
メソッドが例外を再スローすることをアサートする
IEnumerable<DbEntityEntry>
現在の状態では、上記のテスト シナリオをテストすることはできませんDbEntityEntry
。GetDatabaseValues()
などをモックすることはできません。
簡単な解決策は、抽象化の新しいレイヤーを挿入することです。インターフェイスを使用して、現在 catch ブロックにあるコード全体を抽象化し、何もしないモックを提供するとします。
しかし、その後、そのインターフェースの実装をテストしたい状況に陥り、上記と同じ質問をすることになります。、 などをモックするDbUpdateConcurrencyException
にはどうすればよいですか。GetDatabaseValues
私はモッキングにmoqを使用しています。
ご意見ありがとうございます