加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用Machine.Fakes和WithSubject如何在创建主题时告诉框架

发布时间:2020-12-15 08:15:07 所属栏目:百科 来源:网络整理
导读:我想告诉Machine.Fakes框架在创建主题时使用特定值作为构造函数参数 测试对象具有以下构造函数 /// summary /// Initializes a new instance of the see cref="CsvFileRepository{TModel}"/ class. /// /summary /// param name="fileService"The file servi
我想告诉Machine.Fakes框架在创建主题时使用特定值作为构造函数参数

测试对象具有以下构造函数

/// <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参数的特定值.

解决方法

您可以在WithSubject< T>上使用Configure()方法:
Establish context = () =>
    Configure(x => x.For<Func<string,StandardOffer>>()
        .Use(input => new StandardOffer(input)));

以这种方式注册的函数优先于自动模拟.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读