I've been using child containers somewhat successfully for some half-
integration testing (long story).  Recently though I hit a behavior
issue that really kind of shakes my understanding of child
containers.  Basically I am using a child container to temporarily
override some services.  This lets me drop test doubles in for some
half-integration testing.

  I wrote a test that uses child containers in the way I expect, and
it passes.  But if I make some changes the test start to fail.  Maybe
I misunderstand how they work, or maybe this is a bug?  If you look at
the below test, it passes, until any of the three commented lines are
uncommented.  Can someone explain why the change in behavior causes
the test to fail?

Thanks in advance


namespace Castle.Windsor.Tests.Bugs
{
    using System;

    using Castle.MicroKernel.Registration;

    [TestFixture]
    public class fschwiet
    {
        [Test]
        public void parent_usage_changes_child_container_usage()
        {
            WindsorContainer parent = new WindsorContainer();

 
((IWindsorContainer)parent).Register(Component.For(typeof(IParentService)).ImplementedBy(typeof(ParentService)));
 
((IWindsorContainer)parent).Register(Component.For(typeof(IChildService1)).ImplementedBy(typeof(ChildService1)));
 
((IWindsorContainer)parent).Register(Component.For(typeof(IChildService2)).ImplementedBy(typeof(ChildService2)));

            IChildService1 child1;

            //
Assert.IsInstanceOf<ParentService>(parent.GetService<IChildService1>().Child2.Parent);
            //
Assert.IsInstanceOf<ChildService1>(parent.GetService<IChildService1>());
            //
Assert.IsInstanceOf<ChildService2>(parent.GetService<IChildService2>());

            WindsorContainer child = new WindsorContainer();

 
((IWindsorContainer)child).Register(Component.For(typeof(IChildService2)).ImplementedBy(typeof(AnotherChildService2)));

            parent.AddChildContainer(child);

 
Assert.IsInstanceOf<AnotherChildService2>(child.GetService<IChildService2>());

            child1 = child.GetService<IChildService1>();
            Assert.IsInstanceOf<AnotherChildService2>((child1 as
ChildService1).Child2);
        }


        public interface IParentService
        {
        }

        public class ParentService : IParentService
        {
        }

        public interface IChildService1
        {
            IChildService2 Child2 { get;}
        }

        public class ChildService1 : IChildService1
        {
            public IChildService2 Child2 { get; private set; }

            public ChildService1(IChildService2 child2)
            {
                this.Child2 = child2;
            }
        }

        public interface IChildService2
        {
            IParentService Parent { get; }
        }

        public class ChildService2 : IChildService2
        {
            private readonly IParentService xxx;

            public ChildService2(IParentService xxx)
            {
                this.xxx = xxx;
            }

            public IParentService Parent
            {
                get { return xxx; }
            }
        }

        public class AnotherChildService2 : IChildService2
        {
            public IParentService Parent
            {
                get { return null; }
            }
        }

        public class AnotherParentService : IParentService
        {
        }
    }
}

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en.

Reply via email to