Using Castle Windsor in Unit Test Class

I'm a noob with Castle Windsor. I'm building an application that uses Entity Framework 6 and Castle Windsor, with an MSTest Unit Test class. My application has a class in it that implements IWindsorInstaller . My unit test class looks like this:

[TestClass]
public class DatabaseTests {

    static readonly WindsorContainer Container = new WindsorContainer();

    public DatabaseTests() {
        Container.Install( FromAssembly.This() );
    }

    [TestMethod]
    public void FirstTest() {
        // Test statements
    }

    [TestMethod]
    public void SecondTest() {
        // Test statements
    }

    // Other tests
}

There is also an installer class in the unit tests project that looks like this:

public class TestsInstaller : IWindsorInstaller {
    public void Install( IWindsorContainer container, IConfigurationStore store ) {
        container.Install( new RecipeManager.RepositoriesInstaller() );
    }
}

When I go to the Unit Test Session window & try to run all tests, the first one succeeds, and I get this stack trace for the rest:

Unable to create instance of class UnitTests.DatabaseTests. Error: Castle.MicroKernel.ComponentRegistrationException: Component RecipeManager.DAL.CategoryRepository could not be registered. There is already a component with that name. Did you want to modify the existing component instead? If not, make sure you specify a unique name.. at Castle.MicroKernel.SubSystems.Naming.DefaultNamingSubSystem.Register(IHandler handler) at Castle.MicroKernel.DefaultKernel.AddCustomComponent(ComponentModel model) at Castle.MicroKernel.DefaultKernel.Register(IRegistration[] registrations) at Castle.Windsor.WindsorContainer.Register(IRegistration[] registrations) at RecipeManager.RepositoriesInstaller.Install(IWindsorContainer container, IConfigurationStore store) in C:UsersTonydocumentsvisual studio 2015ProjectsRecipeManagerManageRecipesRepositoriesInstaller.cs:line 10 at Castle.Windsor.WindsorContainer.Install(IWindsorInstaller[] installers, DefaultComponentInstaller scope) at Castle.Windsor.WindsorContainer.Install(IWindsorInstaller[] installers) at UnitTests.TestsInstaller.Install(IWindsorContainer container, IConfigurationStore store) in C:UsersTonydocumentsvisual studio 2015ProjectsRecipeManagerUnitTestsTestsInstaller.cs:line 8 at Castle.Windsor.Installer.AssemblyIn staller.Install(IWindsorContainer container, IConfigurationStore store) at Castle.Windsor.WindsorContainer.Install(IWindsorInstaller[] installers, DefaultComponentInstaller scope) at Castle.Windsor.WindsorContainer.Install(IWindsorInstaller[] installers) at UnitTests.DatabaseTests..ctor() in C:UsersTonydocumentsvisual studio 2015ProjectsRecipeManagerUnitTestsDatabaseTests.cs:line 17

If I run the unit tests one at a time, they all succeed. I intend to build lots of tests, so I'd really rather be able to run them all at once. How do I fix this?


In your test class, create a field.

private WindsorContainer Container;

(replace the existing static field.)

Then, add this to your test class:

[TestInitialize]
public void SetUp()
{
    Container = new WindsorContainer();
    // register your dependencies
}

[TestCleanup]
public void Cleanup()
{
    Container.Dispose();
}

[TestInitialize] runs before ever test, [TestCleanup] after every test.
That way you're not reusing the same container for every test and trying to re-register the same dependencies with that container. Before every single test is run you'll be creating a new container. If you need the same dependencies for every test you can register them in Setup() . Or you can register them in the test methods if needed.

链接地址: http://www.djcxy.com/p/59874.html

上一篇: Delphi XE2:FastMM与Win64应用程序一起工作吗?

下一篇: 在单元测试课中使用Castle Windsor