Last active
August 29, 2015 14:24
-
-
Save jchannon/62d8d0231fff8f91ee24 to your computer and use it in GitHub Desktop.
Nancy Testing with MS.Owin.Testing based on work from @damianh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AppRegistrations | |
{ | |
private INancyBootstrapper bootstrapper; | |
public INancyBootstrapper Bootstrapper | |
{ | |
get{ return bootstrapper ?? new MyBootstrapper(); } | |
set{ bootstrapper = value; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>()); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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