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.
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.
[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.
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.
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!