2012-04-01 4 views
6

Machine.Fakesフレームワークに、コンストラクタ引数に特定の値を使用してサブジェクトを作成するよう指示します。Machine.FakesとWithSubjectの使用<TSubject>サブジェクトの作成時に特定のコンストラクタ引数値を使用するようにフレームワークに指示する方法

試験下の対象は、以下のように、私はテストのスタブを作成しました

/// <summary> 
    /// Initializes a new instance of the <see cref="CsvFileRepository{TModel}"/> class. 
    /// </summary> 
    /// <param name="fileService">The file service.</param> 
    /// <param name="repositorySettings">The repository settings.</param> 
    /// <param name="mappingFunction">The mapping function. The mapping function takes in a line from the CSV file and returns the model for said line.</param> 
    public CsvFileRepository(IFileService fileService, IRepositorySettings repositorySettings, Func<string, TModel> mappingFunction) 
    { 
     this.FileService = fileService; 
     this.RepositorySettings = repositorySettings; 
     this.MappingFunction = mappingFunction; 
    } 

次のコンストラクタがあります。

public class when_i_pass_a_csv_file_the_results_are_mapped_to_model_objects : WithSubject<CsvFileRepository<StandardOffer>> 
{ 
    Establish context =() => With(new OffersInFile(new[] { OfferTestData.BobsCsvTestData, OfferTestData.JohnsCsvTestData })); 

    Because of =() => result = Subject.Get(); 

    It should_return_the_same_number_of_fruits_as_there_are_in_the_source_repository =() => result.Count().ShouldEqual(2); 

    static IEnumerable<IOffer> result;        
} 

しかし、Func mappingFunction引数に特定の値を使用するようにMachine.Fakesに指示する方法がわかりません。

答えて

8

あなたはWithSubject<T>Configure()メソッドを使用することができます。

Establish context =() => 
    Configure(x => x.For<Func<string, StandardOffer>>() 
     .Use(input => new StandardOffer(input))); 

機能は、この方法は、オートモックよりも優先され登録されています。

関連する問題