gpt4 book ai didi

c# - 自动夹具 + AutoMoq : Create mock with excluded property

转载 作者:行者123 更新时间:2023-12-04 01:58:31 32 4
gpt4 key购买 nike

例如 ISomething是一个具有三个属性的接口(interface):string Nameint Count和一些复杂的属性ImComplex (具有循环依赖等)我不想建立 AutoFixture。所以我需要 AutoFixture 来创建 ISomething 的 Mock与 NameCount由其默认算法和 ImComplex 设置为空。但是,如果我尝试像这样解决它,我会得到一个异常(exception):

fixture.Customize(new AutoConfiguredMoqCustomization());
var some = fixture.Build<ISomething>().Without(x=>x.ImComplex).Create<ISomething>();

Ploeh.AutoFixture.ObjectCreationException : The decorated ISpecimenBuilder could not create a specimen based on the request: RP.Core.IInformationUnit. This can happen if the request represents an interface or abstract class; if this is the case, register an ISpecimenBuilder that can create specimens based on the request. If this happens in a strongly typed Build expression, try supplying a factory using one of the IFactoryComposer methods.



我该怎么办?

最佳答案

Build禁用所有自定义(如方法文档中所述),因此它不能与 AutoConfiguredMoqCustomization 一起使用.

如果问题是该属性具有循环依赖关系,那么您可以:

  • 重新考虑你的设计(默认情况下,AutoFixture 在发现循环依赖时抛出它的原因,因为这些通常是设计气味)
  • 配置 AutoFixture 以允许循环依赖,达到一定深度
    fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList()
    .ForEach(b => fixture.Behaviors.Remove(b));

    int recursionDepth = 2;
    fixture.Behaviors.Add(new OmitOnRecursionBehavior(recursionDepth));
  • 关于c# - 自动夹具 + AutoMoq : Create mock with excluded property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33973759/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com