It doesn't work for this, because Windsor is trying to create
everything via reflection, and the flags it's passing around are for
only public constructors. InternalsVisibleTo couldn't help here,
either, because my test method and the class with internal
constructors are in the same assembly (Rhino.Testing.Tests.dll)
The patch overrides Windsor's default behaviour to add all public and
non-public constructors to the list of constructor candidates, and it
provides a new activator that will call Activator.CreateInstance with
both public and non-public flags.
Cheers
Matt
On Nov 4, 6:11 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> did you try using the InternalsVisibleTo assembly attribute? i know
> this works for objects, not sure about ctors. this would allow you to
> expose the internal ctors for testing, but not for production.
> upload the patch to the files section of the group. then one of the
> committers can download and apply the patch.
>
> On Nov 4, 12:00 pm, Matt Ellis <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hi folks. I'm currently enjoying the AutoMockingContainer found in
> > RhinoTesting (although a binary download against the current released
> > dlls would be nice).
>
> > I'm missing support for non-public constructors. I want to write code
> > that has a default constructor that provides the dependencies to an
> > internal constructor. In production code, I use the default
> > constructor. In test code, I use the internal constructor. The current
> > AutoMockingContainer doesn't know how to handle this usage - it only
> > works with public constructors.
>
> > This patch allows for internal (and even private!) constructors. Hope
> > the paste works:
>
> > Index: Rhino.Testing.Tests/AutoMocking/AddComponentInstanceFixture.cs
> > ===================================================================
> > --- Rhino.Testing.Tests/AutoMocking/AddComponentInstanceFixture.cs
> > (revision 1715)
> > +++ Rhino.Testing.Tests/AutoMocking/AddComponentInstanceFixture.cs
> > (working copy)
> > @@ -1,31 +1,34 @@
> > using MbUnit.Framework;
> > -using Rhino.Mocks;
> > -using Rhino.Testing.AutoMocking;
> > -
> > -[TestFixture]
> > -public class AddComponentInstanceFixture
> > -{
> > - private AutoMockingContainer container;
> > - private MockRepository mocks;
> > -
> > - public class InstancedComponent
> > - {
> > - }
> > -
> > - [SetUp]
> > - public void SetUp()
> > - {
> > - mocks = new MockRepository();
> > - container = new AutoMockingContainer(mocks);
> > - container.Initialize();
> > - }
> > -
> > -
> > - [Test]
> > - public void AddComponentInstance_CanBeRetrieved()
> > - {
> > - InstancedComponent component = new InstancedComponent();
> > - container.Kernel.AddComponentInstance("InstancedComponent",
> > typeof
> > (InstancedComponent), component);
> > - Assert.AreEqual(component,
> > container.Get<InstancedComponent>());
> > - }
> > +using Rhino.Mocks;
> > +using Rhino.Testing.AutoMocking;
> > +
> > +namespace Rhino.Testing.Tests.AutoMocking
> > +{
> > + [TestFixture]
> > + public class AddComponentInstanceFixture
> > + {
> > + private AutoMockingContainer container;
> > + private MockRepository mocks;
> > +
> > + public class InstancedComponent
> > + {
> > + }
> > +
> > + [SetUp]
> > + public void SetUp()
> > + {
> > + mocks = new MockRepository();
> > + container = new AutoMockingContainer(mocks);
> > + container.Initialize();
> > + }
> > +
> > +
> > + [Test]
> > + public void AddComponentInstance_CanBeRetrieved()
> > + {
> > + InstancedComponent component = new InstancedComponent();
> > +
> > container.Kernel.AddComponentInstance("InstancedComponent",
> > typeof(InstancedComponent), component);
> > + Assert.AreEqual(component,
> > container.Get<InstancedComponent>());
> > + }
> > + }
> > }
> > \ No newline at end of file
> > Index: Rhino.Testing.Tests/AutoMocking/AutoMockingContainerTests.cs
> > ===================================================================
> > --- Rhino.Testing.Tests/AutoMocking/AutoMockingContainerTests.cs
> > (revision 1715)
> > +++ Rhino.Testing.Tests/AutoMocking/AutoMockingContainerTests.cs
> > (working copy)
> > @@ -27,7 +27,40 @@
>
> > }
>
> > - [Test]
> > + // It might be nice to test this on a class in a separate
> > assembly...
> > + [Test]
> > + public void
> > Create_WillResolveDependenciesOnInternalClassWithInternalConstructor()
> > + {
> > + InternalComponentBeingConfigured target =
> > container.Create<InternalComponentBeingConfigured>();
> > + Assert.IsNotNull(target.ReallyCoolService, "mocked
> > ReallyCoolService dependency");
> > + Assert.IsNotNull(target.Services, "mocked Services
> > dependency");
> > + }
> > +
> > + [Test]
> > + public void
> > Create_WillResolveDependenciesPreferringInternalConstructor()
> > + {
> > + ComponentBeingConfiguredWithInternalCtor target =
> > container.Create<ComponentBeingConfiguredWithInternalCtor>();
> > + Assert.IsNotNull(target.ReallyCoolService, "mocked
> > ReallyCoolService dependency");
> > + Assert.IsNotNull(target.Services, "mocked Services
> > dependency");
> > + }
> > +
> > + [Test]
> > + public void
> > Create_WillResolveDependenciesPreferringInternalConstructor_WithResolvePropertiesEnabled()
> > + {
> > + ComponentBeingConfiguredWithInternalCtor target =
> > containerThatResolvesProperties.Create<ComponentBeingConfiguredWithInternalCtor>();
> > + Assert.IsNotNull(target.ReallyCoolService, "mocked
> > ReallyCoolService dependency");
> > + Assert.IsNotNull(target.Services, "mocked Services
> > dependency");
> > + }
> > +
> > + [Test]
> > + public void
> > Create_WillResolveDependenciesPreferringPrivateConstructor()
> > + {
> > + ComponentBeingConfiguredWithPrivateCtor target =
> > container.Create<ComponentBeingConfiguredWithPrivateCtor>();
> > + Assert.IsNotNull(target.ReallyCoolService, "mocked
> > ReallyCoolService dependency");
> > + Assert.IsNotNull(target.Services, "mocked Services
> > dependency");
> > + }
> > +
> > + [Test]
> > public void Create_WillResolveDependenciesAsMocks_ByDefault()
> > {
> > ComponentBeingConfigured target =
> > container.Create<ComponentBeingConfigured>();
> > Index: Rhino.Testing.Tests/AutoMocking/ComponentBeingConfigured.cs
> > ===================================================================
> > --- Rhino.Testing.Tests/AutoMocking/ComponentBeingConfigured.cs
> > (revision 1715)
> > +++ Rhino.Testing.Tests/AutoMocking/ComponentBeingConfigured.cs
> > (working copy)
> > @@ -11,7 +11,6 @@
> > Services = services;
> > }
>
> > -
> > public ComponentBeingConfigured(IReallyCoolService
> > reallyCoolService)
> > {
> > ReallyCoolService = reallyCoolService;
> > Index: Rhino.Testing.Tests/AutoMocking/
> > ComponentBeingConfiguredWithInternalCtor.cs
> > ===================================================================
> > --- Rhino.Testing.Tests/AutoMocking/
> > ComponentBeingConfiguredWithInternalCtor.cs (revision 0)
> > +++ Rhino.Testing.Tests/AutoMocking/
> > ComponentBeingConfiguredWithInternalCtor.cs (revision 0)
> > @@ -0,0 +1,24 @@
> > +namespace Rhino.Testing.Tests.AutoMocking
> > +{
> > + public class ComponentBeingConfiguredWithInternalCtor
> > + {
> > + public IReallyCoolService ReallyCoolService;
> > + public ICollectionOfServices Services;
> > +
> > + internal
> > ComponentBeingConfiguredWithInternalCtor(IReallyCoolService
> > reallyCoolService, ICollectionOfServices services)
> > + {
> > + ReallyCoolService = reallyCoolService;
> > + Services = services;
> > + }
> > +
> > + public
> > ComponentBeingConfiguredWithInternalCtor(IReallyCoolService
> > reallyCoolService)
> > + {
> > + ReallyCoolService = reallyCoolService;
> > + }
> > +
> > + public void RunDispose()
> > + {
> > + Services.SomethingToDispose.Dispose();
> > + }
> > + }
> > +}
> > \ No newline at end of file
> > Index: Rhino.Testing.Tests/AutoMocking/
> > ComponentBeingConfiguredWithPrivateCtor.cs
> > ===================================================================
> > --- Rhino.Testing.Tests/AutoMocking/
> > ComponentBeingConfiguredWithPrivateCtor.cs (revision 0)
> > +++ Rhino.Testing.Tests/AutoMocking/
> > ComponentBeingConfiguredWithPrivateCtor.cs (revision 0)
> > @@ -0,0 +1,23 @@
> > +namespace Rhino.Testing.Tests.AutoMocking
> > +{
> > + public class ComponentBeingConfiguredWithPrivateCtor
> > + {
> > + public IReallyCoolService ReallyCoolService;
> > + public ICollectionOfServices Services;
> > +
> > + private
> > ComponentBeingConfiguredWithPrivateCtor(IReallyCoolService
> > reallyCoolService, ICollectionOfServices services)
> > + {
> > + ReallyCoolService = reallyCoolService;
> > + Services = services;
> > + }
> > +
> > + public
> > ComponentBeingConfiguredWithPrivateCtor(IReallyCoolService
> > reallyCoolService) : this(reallyCoolService, null)
> > + {
> > + }
> > +
> > + public void RunDispose()
> > + {
> > + Services.SomethingToDispose.Dispose();
> > + }
> > + }
> > +}
> > \ No newline at end of file
> > Index: Rhino.Testing.Tests/AutoMocking/
> > InternalComponentBeingConfigured.cs
> > ===================================================================
> > --- Rhino.Testing.Tests/AutoMocking/
> > InternalComponentBeingConfigured.cs (revision 0)
> > +++ Rhino.Testing.Tests/AutoMocking/
> > InternalComponentBeingConfigured.cs (revision 0)
> > @@ -0,0 +1,19 @@
> > +namespace Rhino.Testing.Tests.AutoMocking
> > +{
> > + internal class InternalComponentBeingConfigured
> > + {
> > + public IReallyCoolService ReallyCoolService;
> > + public ICollectionOfServices Services;
> > +
> > + internal InternalComponentBeingConfigured(IReallyCoolService
> > reallyCoolService, ICollectionOfServices services)
> > + {
> > + ReallyCoolService = reallyCoolService;
> > + Services = services;
> > + }
> > +
> > + public void RunDispose()
> > + {
> > + Services.SomethingToDispose.Dispose();
> > + }
> > + }
> > +}
> > \ No newline at end of file
> > Index:
>
> ...
>
> read more »- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Rhino.Mocks" 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/RhinoMocks?hl=en
-~----------~----~----~----~------~----~------~--~---