Question

I've been testing my business logic in ServiceStack 3.9.38 project, and faced a problem when running unit tests separatly leads to success, and running tests all together leads to fail of one of them. After several hours I've made a reproducible unit test. If you run the whole fixture, the second test will fail. Running tests separatly makes them pass.

using Funq;
using NUnit.Framework;

[TestFixture]
public class Test
{
    interface IBar {}
    class Bar : IBar {}
    class TestFoo { public IBar Bar { get; set; } }

    [Test]
    public void Test1()
    {
        var container = new Container();
        var m = new TestFoo();
        container.Register<IBar>(new Bar());
        Assert.NotNull(container.Resolve<IBar>(), "Resolve");
        container.AutoWire(m);
        Assert.NotNull(m.Bar, "Autowire");
    }

    [Test]
    public void Test2()
    {
        var container = new Container();
        var m = new TestFoo();
        container.AutoWire(m);
        Assert.Throws<ResolutionException>(() => container.Resolve<IBar>());
        Assert.IsNull(m.Bar); // FAILS HERE
    }
}

Is that an issue of Funq.Container configuration? Or this is a bug? Any workarounds?

EDIT: I've posted an issue on GitHub: https://github.com/ServiceStack/ServiceStack/issues/521

Was it helpful?

Solution

There is a private static dictionary autoWireCache at AutoWireHelpers.cs that is caching your resolutions When you run the test them twice the value is pulled from the cache and your test fails. I believe the caching is a feature of ServiceStack's customized funq for performance gains.

There is no public interface for clearing that cache so I see no quick solution to the way you have the tests setup.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top