XUnit、RhinoMocks 或 TestDriven.Net 问题 [英] XUnit, RhinoMocks, or TestDriven.Net Issue

查看:34
本文介绍了XUnit、RhinoMocks 或 TestDriven.Net 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在类实例化和 TestDriven.Net(v4.0.3478) 或 XUnit(v2.2.0)、RhinoMocks(v3.6.1) 和 structuremap.automocking(v4.0.0.315) 方面遇到了一些问题.

Having some issues wrapping my head around class instantiation and TestDriven.Net(v4.0.3478) or XUnit(v2.2.0), RhinoMocks(v3.6.1), and structuremap.automocking(v4.0.0.315).

鉴于此代码:

public class Tests1
{
    [Fact]
    public void passing_test()
    {
        var mocker = new RhinoAutoMocker<Subject>();
        mocker.Get<IData>().Stub(x => x.Strings).Return(new List<string> {""});

        var result = mocker.ClassUnderTest.GetStrings();

        result.Count().ShouldEqual(1);
    }
}

public class Tests2
{
    [Fact]
    public void passing_test()
    {
        var mocker = new RhinoAutoMocker<Subject>();
        mocker.Get<IData>().Stub(x => x.Strings).Return(new List<string> {""});

        var result = mocker.ClassUnderTest.GetStrings();

        result.Count().ShouldEqual(1);
    }
}

public class Subject
{
    private readonly IData _data;

    public Subject(IData data)
    {
        _data = data;
    }

    public IEnumerable<string> GetStrings()
    {
        return _data.Strings;
    }
}

public interface IData
{
    IEnumerable<string> Strings { get; set; }
}

当我在特定测试方法或特定类定义上右键单击 -> Run Test(s) 时,所有测试都运行良好.

All tests run fine when I right click -> Run Test(s) on specific test method or a specific class definition.

当我右键单击项目、包含测试的文件夹或上述类的命名空间定义时,测试失败.

Tests fail when I right click on project, folder containing tests or the namespace definition of the class above.

错误是NullReferenceException,在做断言时,它似乎是存根的数据.它是随机的,有时 Tests1.passing_test 失败,有时 Tests2.passing_test 失败.永远不要两者兼而有之.

The errors are NullReferenceException, when doing asserts, it seems to be the stub's data. It's random, sometimes Tests1.passing_test fails, sometimes Tests2.passing_test fails. Never both.

认为它必须与 RhinoAutoMocker 和/或 MockRepository 在测试装置之间没有重置?

Thinking it has to with RhinoAutoMocker and/or the MockRepository not being reset between test fixtures?

UPDATE:简化代码以显示问题,也给出了完整的代码,使用 NUnit [Test] 而不是 XUnit [Fact] 属性有效,一切都表现得像正常.

UPDATE: simplified the code to show the problem, also given code is complete, using NUnit [Test] instead of XUnit [Fact] attributes works, everything behaves as normal.

推荐答案

在您的示例中,您有两个单独的测试类.默认情况下,xUnit v2 将并行运行这些测试.

In your example, you have two separate test classes. By default, xUnit v2 will run these tests in parallel.

我遇到了同样的问题,但在我的情况下使用静态 MockRepository.GenerateMock.在并行测试中使用的静态类会导致异常.测试失败的随机性取决于先运行哪些测试.

I have experienced the same issue, but in my case using the static MockRepository.GenerateMock. The static class being used across the parallel tests results in exceptions. The seeming randomness of the test failures depends on which tests run first.

我可以看到两种选择.1. 在一个班级中进行测试 - 不太可行2. 使用 XUnit Collection 属性将所有测试类放在同一个集合中 - 这对我有用.

There are two alternatives I can see. 1. Tests in a single class - not really workable 2. Use the XUnit Collection attribute to place all tests classes in the same collection - this worked for me.

参见:http://xunit.github.io/docs/running-tests-in-parallel.html

另一种选择是在测试程序集中使用以下属性关闭 xUnit 的并行性

Another alternative is to turn off parallelism for xUnit using the following attribute in your test assembly

[程序集:CollectionBehavior(DisableTestParallelization = true)]

这篇关于XUnit、RhinoMocks 或 TestDriven.Net 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆