2012-03-28 6 views
1

構造マップの設定に汎用/オンラインステートメントが必要です。次のコードを参照し、変更を提案してください:ユニットテスト - MOQでStructure Mapを使用

のStructureMapの設定クラス:

class StructureMapTestConfigurationRegistry : Registry 
    { 
     /// <summary> 
     /// Initializes a new instance of the <see cref="DependencyConfigurationRegistry"/> class. 
     /// </summary> 
     public StructureMapTestConfigurationRegistry() 
     { 

      For<Repository.IRepository<Report>>().Use(MockObjectGenerator<Report>.RepositoryMockSetup()); 
      For<Repository.IRepository<RelatedContent>>().Use(MockObjectGenerator<RelatedContent>.RepositoryMockSetup()); 
      For<Repository.IRepository<Tags>>().Use(MockObjectGenerator<Tags>.RepositoryMockSetup()); 

      For<Repository.IRepository<ArticleTag>>().Use(MockObjectGenerator<ArticleTag>.RepositoryMockSetup()); 
      For<Repository.IRepository<ReferenceBookTag>>().Use(MockObjectGenerator<ReferenceBookTag>.RepositoryMockSetup()); 
      For<Repository.IRepository<EventsTag>>().Use(MockObjectGenerator<EventsTag>.RepositoryMockSetup()); 
      For<Repository.IRepository<CountryFactfileTag>>().Use(MockObjectGenerator<CountryFactfileTag>.RepositoryMockSetup()); 
} 

ブートストラップクラス:

public static class TestBootstrapper 
    { 
     public static void TestConfigureStructureMap() 
     { 
      ObjectFactory.Container.Dispose(); 
      ObjectFactory.Initialize(o => o.AddRegistry(new StructureMapTestConfigurationRegistry())); 
      ObjectFactory.Container.AssertConfigurationIsValid(); 

     } 
} 

MockObjectGeneratorクラス:

public static class MockObjectGenerator<TEntity> where TEntity : class 
    { 

     private static List<TEntity> DummyTable 
     { 
      get 
      { 
       return MockEntities.GetData<TEntity>(); 
      } 
     } 

     public static IRepository<TEntity> RepositoryMockSetup() 
     { 
      Mock<IRepository<TEntity>> repository = new Mock<IRepository<TEntity>>(MockBehavior.Strict); 
      repository.Setup(o => o.Fetch(It.IsAny<Expression<Func<TEntity, bool>>>())).Returns((Expression<Func<TEntity, bool>> i) => DummyTable.Where(i.Compile()).ToList()); 
      repository.Setup(o => o.Create(It.IsAny<IEnumerable<TEntity>>())).Callback<IEnumerable<TEntity>>(items => DummyTable.AddRange(items)); 
      repository.Setup(o => o.Delete(It.IsAny<TEntity>())).Callback<TEntity>(item => DummyTable.Remove(item)); 

} 
} 

**Mock Entities Class :** 

public static class MockEntities 
    { 
     public static Dictionary<string, dynamic> MockData = new Dictionary<string, dynamic>(); 

     public static void LoadData() 
     { 
      MockData.Add(typeof(CMSModel.Article).Name, ArticleTestData.GetTestRecords()); 
      MockData.Add(typeof(CMSModel.ArticleTag).Name, RelatedArticleContentTestData.GetTestRecords()); 
} 

public static List<T> GetData<T>() where T : class 
     { 
      return (List<T>)MockData[typeof(T).Name];  
     } 
} 

注:実際のユニットテストでは、我々はのStructureMapを使用して、依存オブジェクトを作成しながら、それが行われているので、セットアップ方法を記述するために持っていけないよう

これが行われています。

それは正常に動作しますが、私は一般的な

にコンフィギュレーションファイルのコードをリファクタリングしたい私はこのようなimplentationのためにそれを書かれている:

For(typeof(Repository.IRepository<>)).Use(typeof(Repository.Repository<>)); 

は、ユニットテスト構成のため、それは可能ですか?

答えて

0

使用するすべてのタイプのエンティティタイプを指定し、それらをタイプとして渡す必要がありますが、可能です。そして、汎用でないバージョンの関数を使う必要があります(Forの代わりにFor(type)を書くことができるようにするため)。そうすれば、すべての関数は型変数に対してのみ機能し、単純なメソッドのように見えます。

+0

これは次のように書くことができます: )>()の場合、typeof(MockObjectGenerator <>。RepositoryMockSetup()));を使用します。 – mak

+0

いいえ、できません。あなたはジェネリックスをあきらめ、あなたのモックをセットアップするために(タイプ)のためにいつも使うべきです。 –

関連する問題