Skip to content

Instantly share code, notes, and snippets.

@eugeneagafonov
Created June 6, 2012 16:05
Show Gist options
  • Save eugeneagafonov/2882940 to your computer and use it in GitHub Desktop.
Save eugeneagafonov/2882940 to your computer and use it in GitHub Desktop.
How to run unit tests in new web api builds
// assuming we have an IoC container. Autofac example
var builder = new ContainerBuilder();
// assuming your global.asax.cs defines WebApiApplication : HttpApplication
builder.RegisterApiControllers(typeof(WebApiApplication).Assembly);
var container = builder.Build();
// getting the controller
var target = container.Resolve<MyController>();
var request = new HttpRequestMessage();
request.Properties[HttpPropertyKeys.HttpConfigurationKey] = new HttpConfiguration();
target.Request = request;
// now we can test
var correct = target.Get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment