问题
Hi there Im using windsor as a DI container,
my code is below
public static class ContainerBuilder
{
public static IWindsorContainer Build()
{
var container = new WindsorContainer("Configuration\\Windsor.config");
// automatically register controllers
container.Register(AllTypes
.Of<Controller>()
.FromAssembly(Assembly.GetExecutingAssembly())
.Configure(c => c.LifeStyle.Transient.Named(c.Implementation.Name.ToLower())));
container.Register(
Component.For<IServiceLocator>().Instance(new WindsorServiceLocator(container)),
Component.For(typeof(IRepository<>)).ImplementedBy(typeof(NHibernateRepository<>)).LifeStyle.Transient
);
return container;
}
}
I need to call this from a test project , the problem is that when I do this the windsor.config is never found and the test seems to always fail, where is the best way to place this config file or is there a better approach to doing this? Thanks
回答1:
Just make the config path configurable, e.g.
public static IWindsorContainer Build(string configPath) {
var container = new WindsorContainer(configPath);
...
}
In your app the configPath is "Configuration\Windsor.config" while in your tests you'll have a path like "....\Configuration\Windsor.config".
Note that you shouldn't generally depend on the container in your tests, unless you're running some integration tests.
Also a static container builder doesn't seem like a good idea, take a look at Windsor Installers to register your components in a modular way.
来源:https://stackoverflow.com/questions/3202525/asp-net-mvc-test-project-cannot-find-windsor-file