私はキャッスルウィンザーのノブです。私はEntity Framework 6とCastle Windsorを使用するアプリケーションをMSTest Unit Testクラスと共に構築しています。私のアプリケーションには、IWindsorInstaller
を実装するクラスがあります。ユニットウィンザーをユニットテストで使用するクラス
[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
}
インストーラクラスは次のようになりますユニットテストプロジェクトでもあります:私はしようと&ユニットテストセッションウィンドウに行くとき
public class TestsInstaller : IWindsorInstaller {
public void Install(IWindsorContainer container, IConfigurationStore store) {
container.Install(new RecipeManager.RepositoriesInstaller());
}
}
私のユニットテストクラスは、このようになります。最初の1が成功し、すべてのテストを実行し、私は休みのため、このスタックトレースを取得する:私は一度ユニットテスト1を実行する場合
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
、それらはすべて成功します。私はたくさんのテストを作ろうと思っているので、一度にそれらをすべて実行することができます。これをどうやって解決するのですか?
私はキャッスルを使ってテスト用の依存関係注入を処理するのが実際には悪い考えかと思います。 1)単体テストの自己文書化の原則に違反している。なぜなら、依存関係が隠されているからである.2)依存関係を変更してもコードはコンパイルされ、潜在的にコードが明らかになる。 – gdbj