Issues with servicestack, testing, and xunit class fixtures

Hello.

I’ve been having some issues with one of my servicestack solutions,
where I’m trying to do something more akin to integration testing.
I.e. I want to keep an apphost around for all tests in a test class,
so that’s where xUnit’s IClassFixture comes into the picture.

According to their documentation, a class fixture is supposed to be created
once for every test class, and it’s supposed to be cleaned up after.
Sounds good, and if I test a single class taking a IClassFixture<AppHostFixture>, it works fine.
However, when I add another test class, I start getting a bunch of errors that seem to be related
to disposing/creation of the AppHost.
E.g.:

  • System.IO.InvalidDataException : ServiceStackHost.Instance has already been set (AppHostFixture)
  • Funq.ResolutionException : Required dependency of type ServiceStack.Data.IDbConnectionFactory could not be resolved.
  • System.ObjectDisposedException : Cannot access a disposed object.

I have a repository here that reproduces these errors: GitHub - thomasbrenna/servicestack-xunit-test-issue-reproduction: servicestack xunit test issue reproduction.
Here’s also an example of some of the errors: Remove specific ormlite version (prerelase) · thomasbrenna/servicestack-xunit-test-issue-reproduction@d9fc838 · GitHub

Is there something wrong I am doing in terms of disposing the AppHost?

The reason I’m using multiple test classes with the AppHostFixture is
because I am testing different things.
The scope of these tests also vary (the example repro is perhaps a bit contrived, but is able to reproduce the issue).

I actually tried to have just a DbFixture for certain tests, using ormlite,
but there I quickly hit the “10 table limit” for ormlite free,
and I couldn’t figure out how to add a license when using just ormlite (i.e. not instantiating an entire apphost).
You wouldn’t happen to have any examples for how to use JUST ormlite, when I’ll hit the 10 tables limit?
This is for example relevant if I have certain service (not servicestack service) classes that might take
a IDbConnectionFactory, where an entire AppHost is really overkill.

Refer to Testing Docs for info on Integration and Unit tests.

E.g. there can only be a single AppHost instance registered at any one time, after the testing scope is finished the AppHost needs to be disposed.

When the scope is limited to a Test:

[Test]
public void My_unit_test()
{
    using (new BasicAppHost().Init())
    {
        //test ServiceStack classes
    }
}

Or when the scope is for an entire Test Fixture:

public class MyUnitTests
{
    ServiceStackHost appHost;
    // Run before Test Fixture starts
    public MyUnitTests() => appHost = new BasicAppHost().Init();

    [OneTimeTearDown] // Run after Test Fixture has completed
    public void OneTimeTearDown() => appHost.Dispose();

    [Test]
    public void My_unit_test()
    {
        //test ServiceStack classes
   }
}

Either way the AppHost needs to be explicitly disposed.

It is disposed here: servicestack-xunit-test-issue-reproduction/ReproduceTestIssue.Tests/Fixtures/AppHostFixture.cs at master · thomasbrenna/servicestack-xunit-test-issue-reproduction · GitHub, but that doesn’t seem to work properly.
If that looks right to you (it seems to be that same that is done in one of your examples on integration tests), then it might be more of an xUnit problem than a Servicestack problem I guess.

What about the ormlite issue I mentioned?
I.e. hitting the table limit quickly, and how to register a license when using just ormlite.

Make sure the tests aren’t being run in parallel, i.e. try debugging them to make sure the AppHost is being created and disposed sequentially.

You need to register the License Key, if it’s not configured in the SERVICESTACK_LICENSE Environment Variable it should be registered in Licensing.RegisterLicense(licenseKeyText); before the AppHost is initialized.

Do I need an AppHost for the license to be registered?
Or is it enough to set just set SERVICESTACK_LICENSE?
Say I want to use just ormlite for more narrow tests.

You don’t need an AppHost, and can use any License Registration option.

The tips about parallelisation seems to fix it for the reproduction project, but not in my actual project.
Annoying, but it seems to be more of an xUnit issue than servicestack, so that’s not your area of course ; )

I’ll see if I can get a DbFixture working by setting the SERVICESTACK_LICENSE env var.
Thanks!