Skip to content

Instantly share code, notes, and snippets.

@jchannon
Last active August 29, 2015 14:24
Show Gist options
  • Save jchannon/62d8d0231fff8f91ee24 to your computer and use it in GitHub Desktop.
Save jchannon/62d8d0231fff8f91ee24 to your computer and use it in GitHub Desktop.
Nancy Testing with MS.Owin.Testing based on work from @damianh
public class AppRegistrations
{
private INancyBootstrapper bootstrapper;
public INancyBootstrapper Bootstrapper
{
get{ return bootstrapper ?? new MyBootstrapper(); }
set{ bootstrapper = value; }
}
}
public class MyTest
{
[Fact]
public async Task Should_Return_200_On_Allowed_Path()
{
//Given
var client = this.CreateHttpClient();
//When
var response = await client.PostAsync("http://localhost/api/myroute", new StringContent(string.Empty));
//Then
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
private HttpClient CreateHttpClient()
{
var setup = new OwinTestSetup();
return setup.CreateHttpClient(with =>
{
with.Module<SystemModule>();
with.Dependency<ISomethingCool>(A.Fake<ISomethingCool>());
});
}
}
public class OwinTestSetup
{
public HttpClient CreateHttpClient(Action<ConfigurableBootstrapper.ConfigurableBootstrapperConfigurator> configureBootstrapperAction)
{
var appRegistrations = new AppRegistrations();
appRegistrations.Bootstrapper = new ConfigurableBootstrapper(configureBootstrapperAction);
var client = TestServer.Create(builder =>
new Startup(appRegistrations)
.Configuration(builder))
.HttpClient;
client.DefaultRequestHeaders.Add("Accept", "application/json");
return client;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment