Search code examples
c#unit-testingxunitsimple-injector

Test fails duing run all tests, but running them one by one successes


I have 4 tests spread across 3 test classes. If I run each test one by one they all can succeed. But running all (parallel I think?) they all fail except the first one fired?

My tests require the same setup, so I have a fixture which all tests are set up with:

public class CompositionRootFixture
{
    public Container Container { get; private set; } // Simple Injector Container

    public CompositionRootFixture()
    {
        Container = new Container();

        /* code removed for clearity */

        Container.Verify();
    }
}

And is used in my test classes like so:

public class CommandProcessorTests : IClassFixture<CompositionRootFixture>
{
    private readonly CompositionRootFixture _fixture;

    public CommandProcessorTests(CompositionRootFixture fixture)
    {
        _fixture = fixture;
    }

    [Fact]
    public async Task TestExecutingUsingContainerForResolution()
    {
        var commands = _fixture.Container.GetInstance<IExecuteCommands>();
        var command = new FakeCommandWithoutValidator();
        await commands.Execute(command);

        Assert.Equal("faked", command.ReturnValue);
    }
}

I have a hard time figuring out how to use the IClassFixture<T> and the documentation is not very helpful in setting this up. I am using the latest XUnit 2.0.0-beta5-build2785.

Failed description:

---- System.InvalidOperationException : The configuration is invalid. Creating the instance for type IHandleCommand<FakeCommandWithoutValidator> failed. The registered delegate for type IHandleCommand<FakeCommandWithoutValidator> threw an exception. The configuration is invalid. The type HandleFakeCommandWithoutValidator is directly or indirectly depending on itself.
-------- SimpleInjector.ActivationException : The registered delegate for type IHandleCommand<FakeCommandWithoutValidator> threw an exception. The configuration is invalid. The type HandleFakeCommandWithoutValidator is directly or indirectly depending on itself.
------------ SimpleInjector.ActivationException : The configuration is invalid. The type HandleFakeCommandWithoutValidator is directly or indirectly depending on itself.
---- The following constructor parameters did not have matching fixture data: CompositionRootFixture fixture

Solution

  • I fixed this question by upgrading to xUnit 2.0.0 and using the new Collection Fixture, which is described on their website: http://xunit.github.io/docs/shared-context.html