I have a Web API 2 application I would like to add some tests hosting the server in memory. I have this configuration in my tests.
public BaseServerTests()
{
var fake = Substitute.For<IFooService>();
_server = TestServer.Create(app =>
{
var configuration = new HttpConfiguration();
var container = new StandardKernel();
container.Bind<IFooService>().ToConstant(fake);
configuration.DependencyResolver = new NinjectDependencyResolver(container);
WebApiConfig.Register(configuration);
app.UseWebApi(configuration);
});
_client = _server.HttpClient;
}
The controller has only one constractor like this.
public FooController(IFooService fooSrv)
{
_fooSrv = fooSrv;
}
The Ninject configuration in the Web Api looks like this.
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(NinjectWebCommon), "Stop")]
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
I am getting the following error.
Ninject.ActivationException: 'Error activating ModelValidatorProvider using binding from ModelValidatorProvider to NinjectDefaultModelValidatorProvider A cyclical dependency was detected between the constructors of two services.
Is this the correct way of setting up the fake dependencies with Ninject?
I was doing it totally wrong. I got it working now. I had to install the package Ninject.Web.WebApi.OwinSelfHost.
public BaseServerTests()
{
var fake = Substitute.For<ISomeService>();
fake.SomeCall().Returns("this is totally fake");
_server = TestServer.Create(app =>
{
var configuration = new HttpConfiguration();
var container = new StandardKernel();
container.Bind<ISomeService>().ToConstant(fake);
WebApiConfig.Register(configuration);
app.UseNinject(() => container);
app.UseNinjectWebApi(configuration);
});
_client = _server.HttpClient;
}