Skip to content

Instantly share code, notes, and snippets.

@Alexei000
Created March 28, 2018 15:19
Show Gist options
  • Save Alexei000/2fc303c5412f579304643815fc5ab8c3 to your computer and use it in GitHub Desktop.
Save Alexei000/2fc303c5412f579304643815fc5ab8c3 to your computer and use it in GitHub Desktop.
Register custom services
protected void RegisterCustomServicesForServiceTesting()
{
// IRepository<T> should be solved using InMemoryRepository<T>, by default
Kernel.Rebind(typeof(IRepository<>)).To(typeof(InMemoryRepository<>));
// IRepository<T> must be solved to Repository<T>, if used in CachedRepository<T>
Kernel.Rebind(typeof(IRepository<>)).To(typeof(InMemoryRepository<>)).WhenInjectedInto(typeof(CachedRepository<>));
var nonCachedTypes = NinjectCommon.GetNonCachedTypes();
nonCachedTypes.ForEach(type =>
{
Type memoryRepositoryType = typeof(InMemoryRepository<>).MakeGenericType(type);
var repoType = typeof(IRepository<>).MakeGenericType(type);
// allow access as normal repository
Kernel.Rebind(repoType).To(memoryRepositoryType);
});
Kernel.Rebind<IScopedDataAccess>().To<ScopedDataAccess>().InSingletonScope();
Kernel.Rebind<ILoggingService>().To<InMemoryLoggingService>().InSingletonScope();
Logger = Kernel.Get<ILoggingService>() as InMemoryLoggingService;
var mockConfig = Substitute.For<IConfigService>();
Kernel.Rebind<IConfigService>().ToConstant(mockConfig).InSingletonScope();
Kernel.Rebind<IEnvironmentProxyService>().To<EnvironmentProxyServiceMock>();
Kernel.Rebind<IEnvironmentDatabaseProxyService>().To<EnvironmentDatabaseProxyServiceMock>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment