jeudi 28 avril 2016

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:\Users\Tony\documents\visual studio 2015\Projects\RecipeManager\ManageRecipes\RepositoriesInstaller.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:\Users\Tony\documents\visual studio 2015\Projects\RecipeManager\UnitTests\TestsInstaller.cs:line 8 at Castle.Windsor.Installer.AssemblyInstaller.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:\Users\Tony\documents\visual studio 2015\Projects\RecipeManager\UnitTests\DatabaseTests.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?

Aucun commentaire:

Enregistrer un commentaire