Search code examples
unit-testingrhino-mocksxunittestdriven.netstructuremap-automocking

XUnit, RhinoMocks, or TestDriven.Net Issue


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).

Given this code:

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; }
}

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.

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.

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

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.


Solution

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

    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.

    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.

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

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

    [assembly: CollectionBehavior(DisableTestParallelization = true)]