2012-04-01 18 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;        
} 

但我不知道如何告訴Machine.Fakes使用Func mappingFunction參數的特定值。

回答

8

您可以在WithSubject<T>使用Configure()方法:

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

功能註冊這樣擁有自動嘲諷的優先級。

相關問題