0

我有一个用 xUnit、AutoFixture 编写的单元测试,使用AutoFixture Idioms GuardClauseAssertion检查我的程序集类中的保护子句:

[InlineData(typeof (ProjectOneClass))]
[InlineData(typeof (ProjectTwoClass))]
[InlineData(typeof (ProjectThreeClass))]
[InlineData(typeof (ProjectFourClass))]
[InlineData(typeof (ProjectFiveClass))]
[Theory(DisplayName = "Classes guard against null arguments in assemblies")]
public void GuardAgainstNullArgumentsInAssemblies(Type assemblyHintType)
{
    // arrange
    IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());
    fixture.Behaviors.Add(new TracingBehavior());
    var assertion = new GuardClauseAssertion(fixture);

    // assert
    assertion.Verify(assemblyHintType.Assembly.GetTypes()
        .Where(t =>
            !t.Name.Equals("MyWebServices") &&
            !t.Name.Equals("MyComputer") &&
            !t.Name.Equals("MyProject") &&
            !t.Name.Equals("MyApplication") &&
            !t.IsInterface &&
            !t.IsGenericType &&
            !t.IsCompilerGenerated() &&
            !t.IsStaticClass()
        ));
}

我使用过滤器断言单元测试只关注我编写的适当类,并使用提示类型来定位特定程序集。我在 using 中传递了这些提示类型InlineData(typeof(...)),以便对每种提示类型进行一次测试。

在进行了几次测试运行后,测试通过了 5 次测试中的 3 次,这些测试指出了我忘记放置空保护的代码,但其他两个测试产生以下异常:

System.BadImageFormatException 尝试加载格式不正确的程序。(来自 HRESULT 的异常:0x8007000B)在

System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type) 在 System.Reflection.Emit.TypeBuilder.CreateTypeNoLock() 在 System.Reflection.Emit.TypeBuilder.CreateType() 在 Ploeh.AutoFixture.Idioms。 GuardClauseAssertion.AutoGenericArgument.get_Value() 在 System.Linq.Enumerable.WhereSelectArrayIterator 2.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray(IEnumerable1 来源)在 Ploeh.AutoFixture.Idioms.IdiomaticAssertion.Verify(MethodInfo[] methodInfos) 在 Ploeh.AutoFixture.Idioms.GuardClauseAssertion.Verify(MethodInfo methodInfo) 在 Ploeh.AutoFixture.Idioms.GuardClauseAssertion.AutoGenericMethod.get_Value()。 AutoFixture.Idioms.IdiomaticAssertion.Verify(Type type) 在 NullGuardTests.GuardAgainstNullArgumentsInNonModelAssemblies(Type assemblyHintType) 在 NullGuardTests.cs: line 58

我添加TracingBehaviorIFixture实例以获得更多可见性,它显示:

Requested: Infrastructure.Logging.IErrorAggregator errorAggregator
  Requested: Ploeh.AutoFixture.Kernel.SeededRequest

……………………………………………………………………………………………………………………………………………………………………

我查看了这条长跟踪中的第一个项目和最后一个项目,试图找出它们与成功测试跟踪中显示的第一个/最后一个项目有何不同,但我无法找到任何有意义的差异。

我假设跟踪中的最后一项是IFixture异常之前的最后一个活动。

除了我迄今为止所做的之外,我如何更深入地了解这样的异常?

  • 每个目标程序集都以 .NET 4.0 为目标
  • 每个目标程序集在构建配置 (AnyCPU) 中都有相同的设置
  • 每个目标程序集都是用 VB.NET 编写的(尽管单元测试是用 C# 编写的)

想法?先感谢您。

2014 年 3 月 29 日更新

我仍然无法弄清楚这里发生了什么,但我希望留下这个问题至少是对其他有同样问题的人的理智检查,他们在这里发现了这个问题。在工作量允许的情况下,我将努力提交一份复制品。

4

0 回答 0