Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Machine.Fakes and WithSubject<TSubject> how do you tell the framework to use a specific constructor argument value when creating the subject

I would like to tell the Machine.Fakes framework to use a specific value for a constructor argument when creating the subject

The subject under test has the following constructor

    /// <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;
    }

I've created the stub of a test as follows:

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;                            
}

But I'm not sure how to tell Machine.Fakes to use a specific value for the Func mappingFunction argument.

like image 568
Edward Wilde Avatar asked Apr 01 '12 10:04

Edward Wilde


1 Answers

You can use the Configure() method on WithSubject<T>:

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

The function registered this way has precedence over auto mocking.

like image 195
Simon Hohenadl Avatar answered Oct 20 '22 06:10

Simon Hohenadl