Skip to content

Instantly share code, notes, and snippets.

@dealproc
Created January 25, 2016 20:08
Show Gist options
  • Save dealproc/8716eb9f78f0d62bb762 to your computer and use it in GitHub Desktop.
Save dealproc/8716eb9f78f0d62bb762 to your computer and use it in GitHub Desktop.
Sample Fixture for an owin test server
using Microsoft.Owin.Testing;
using System;
using System.Linq;
using System.Net.Http;
using Xunit;
namespace {Your Project}.Tests.Fixtures {
[CollectionDefinition("Test Server")]
public class TestServerFixtureCollection : ICollectionFixture<TestServerFixture> {
// This class has no code, and is never created. Its purpose is simply
// to be the place to apply [CollectionDefinition] and all the
// ICollectionFixture<> interfaces.
}
public class TestServerFixture : IDisposable {
bool _disposed = false;
public TestServer Server { get; private set; }
public TestServerFixture() {
Server = TestServer.Create<{your owin startup class here}>();
Server.BaseAddress = new Uri("https://localhost/");
}
~TestServerFixture() {
Dispose(false);
}
public HttpClient CreateClient() {
return Server.HttpClient;
}
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
if (!disposing || _disposed) { return; }
if (Server != null) {
Server.Dispose();
Server = null;
}
_disposed = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment