【问题标题】:How can I find out why AutoFixture is throwing Kernel.OmitSpecimen exception如何找出 AutoFixture 抛出 Kernel.OmitSpecimen 异常的原因
【发布时间】:2012-09-21 13:52:11
【问题描述】:

我正在研究一个相当嵌套的模型,它有一些循环引用。它还使用实体框架,因此所有列表都是ICollection<T>。为了适应这一点,我正在像这样配置 AutoFixture:

_fixture = new Fixture().Customize(new MultipleCustomization());
_fixture.Behaviors.Remove(new ThrowingRecursionBehavior());
_fixture.Behaviors.Add(new OmitOnRecursionBehavior());

当我尝试创建类型时

_fixture.CreateAnonymous<Session>();

AutoFixture有问题,抛出如下错误

System.InvalidCastException:无法将“Ploeh.AutoFixture.Kernel.OmitSpecimen”类型的对象转换为“The.Model.Language”类型

如果我排除 SessionLanguage 类型的集合,AutoFixture 会为图中的另一种类型抛出相同的异常。

有没有办法从 AutoFixture 中提取更多信息,例如导致错误的属性?

为什么 AutoFixture 试图将我的类型转换为 OmitSpecimen?在此过程中可能发生了什么阻止它被转换?

我为堆栈跟踪 here 创建了一个要点。

更新

我已经设法重现了这个问题。给定这对对象

public class Session
{
    public Language Language { get; set; }
}

public class Language
{
    public ICollection<Session> Sessions { get; set; }
}

_fixture.CreateAnonymous&lt;Session&gt;(); 的调用将抛出强制转换异常。

【问题讨论】:

  • 你能发布堆栈跟踪以及异常消息吗?
  • 跟踪 AutoFixture 内部发生的事情的一种方法是添加 TracingBehavior:fixture.Behaviors.Add(new TracingBehavior()); 默认情况下它将写入 Console.Out,但您可以跟踪到任何 TextWriter。
  • 我在问题中添加了指向堆栈跟踪的链接。谢谢你的追踪提示,我会玩的。
  • 我希望我现在已经设法解决了这个问题。您可以尝试使用 AutoFixture 2.12.1(或更高版本)看看是否有帮助吗?
  • 所有这些努力,但没有回答投票...

标签: autofixture


【解决方案1】:

这适用于较新版本的 AutoFixture(例如 3.31.3)。

此代码确实运行:

    public class Session
    {
        public Language Language { get; set; }
    }

    public class Language
    {
        public ICollection<Session> Sessions { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var fixture = new Fixture();
            fixture.Behaviors.Add(new OmitOnRecursionBehavior());

           var session = fixture.Create<Session>();

            Debug.Assert(session != null, "Session should not be null");
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 2017-08-02
    • 2013-05-24
    相关资源
    最近更新 更多