0
私はラッパーを介してコンテキストを使用する状況があります。ラッパーはサービスレイヤーで使用されます。問題は、この特定のケースでコンテキストをモックする方法です。ラッパーを介してコンテキストをモックする方法
コンテキスト
public class DbContext : System.Data.Entity.DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
...
base.OnModelCreating(modelBuilder);
}
public DbSet<Report> Reports { get; set; }
}
ラッパー
public interface IDbContextWrapper<out TContext> where TContext : System.Data.Entity.DbContext, new()
{
T Call<T>(Func<TContext, T> func);
void Call(Action<TContext> action);
}
ラッパーの実装
public class DbContextWrapper<TContext> : IDbContextWrapper<TContext> where TContext : System.Data.Entity.DbContext, new()
{
public virtual TContext BuildContext(bool enableChangeTracking = true, bool enableLazyLoading = false, bool enableProxyCreation = false)
{
var result = new TContext();
result.Configuration.AutoDetectChangesEnabled = enableChangeTracking;
result.Configuration.LazyLoadingEnabled = enableLazyLoading;
result.Configuration.ProxyCreationEnabled = enableProxyCreation;
return result;
}
public virtual T Call<T>(Func<TContext, T> func)
{
using (var context = BuildContext())
{
return func(context);
}
}
public virtual void Call(Action<TContext> action)
{
using (var context = BuildContext())
{
action(context);
}
}
}
そして、私はモック
でユニットテストに必要なサービスとその方法