jeudi 21 mai 2015

ServiceStack integration tests: ServiceStackHost.Instance has already been set

I am writing integration tests for ServiceStack with in-memory database and I ran into this exception: "System.IO.InvalidDataException ServiceStackHost.Instance has already been set" while trying to run multiple test classes together, each having its own AppHostHttpListenerBase. However, if I ran the test classes one at a time, it ran and passed without problems. One reason for having multiple classes is because I want to test the AppHost with different services/dependencies registered and also to group my tests logically. Below is a general snippet of my tests. I would like to be able run all the test at one go.

public class TestClassOne : IDisposable
{
    string _endPoint = "http://localhost:54321/";
    AppHostHttpListenerBase _appHost;
    IDbConnectionFactory _dbConn = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider);

    public TestClassOne()
    {
        _appHost = new UnitTestAppHost(_dbConn, ...){};
        _appHost.Init().Start(_endPoint);
    }

    [Fact]
    public void Test()
    {
       ...
       using(var db = _dbConn.Open())
       {
          Assert.True(...);
       }
    }

    public void Dispose()
    {
        _appHost.Dispose();
        _appHost = null;
    }    
}

public class TestClassTwo : IDisposable
{
    string _endPoint = "http://localhost:54321/";
    AppHostHttpListenerBase _appHost;
    IDbConnectionFactory _dbConn = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider);

    public TestClassTwo()
    {
        _appHost = new UnitTestAppHost(...){};
        _appHost.Init().Start(_endPoint);
    }

    [Fact]
    public void Test()
    {
       ...
       using(var db = _dbConn.Open())
       {
          Assert.True(...);
       }
    }

    public void Dispose()
    {
        _appHost.Dispose();
        _appHost = null;
    }    
}

I have tried running on another AppDomain, but it doesn't seems to be what I am looking for I think, because I need to do some Asserts on IDbConnection in the current running AppDomain (?), if that make any sense. Any suggestions on how I should be doing it? I'm using xUnit and Resharper's test runner btw.

Aucun commentaire:

Enregistrer un commentaire