Yikes! Thanks anyhow.

On Sep 9, 7:50 pm, Ayende Rahien <[email protected]> wrote:
> No, it is not
>
> On Wed, Sep 9, 2009 at 9:47 PM, bill richards
> <[email protected]>wrote:
>
>
>
>
>
> > Is it possible to mock extension methods using RM v 3.5?
>
> > It seems that my problem revolves around the fact
> > that .RegisterViewWithRegion() is an extension method.
>
> > On Sep 9, 4:57 pm, Alex McMahon <[email protected]> wrote:
> > > A few observations:
> > > 1. The usage pattern for Unity seems a bit odd. I wouldn't expect to
> > > see any references to unity in the majority of an application's
> > > classes. One of the main points about IoC such as Unity is that it
> > > automatically resolves dependencies so if you changed your
> > > StatusBarModule constructor to take and IRegionManager and an
> > > IEventAggregator then when unity tries to build your StatusBarModule
> > > it will automatically populate those construction parameters.
> > > 2. Similarly with the call to Resolve<IStatusBarViewPresenter> this
> > > could/should be replaced with a dependency that is populated by unity
> > > (probably in the constructor).
> > > 3. Once you replace the constructor parameters your test can get rid
> > > of the MockUnityContainer and just create MockRegionManager (and other
> > > dependencies), pass it in explicitly in the constructor (Note that
> > > during the test Unity will not be used at all) Then in the actual test
> > > you can just write
> > > regionManager.AssertWasCalled(x=>x.RegisterViewWithRegion(....)
>
> > > I must admit my knowledge of Prism and Unity is patchy, but with CAB
> > > (the precursor to Prism) this was how we worked.
>
> > > Try only having one place in your application aware of Unity, and let
> > > all the dependencies of your object graph be populated by unity. This
> > > will leave your application much less tightly bound to Unity.
>
> > > 2009/9/9 bill richards <[email protected]>:
>
> > > > Alex,
>
> > > > thanks for that, and the point is conceeded:
>
> > > > The code as posted represents the "Arrange" aspect of the AAA test,
> > > > and the remainder was not posted, because at the time of posting, it
> > > > seemed superfluous to the issue I am trying to resolve.
>
> > > > I also agree that there are quite a number of items being stubbed, but
> > > > this is due to the complexity of the framework (Prism V2).
>
> > > > This is class definition for the object under test:
>
> > > >    public class StatusBarModule
> > > >    {
> > > >        public StatusBarModule(IUnityContainer container)
> > > >        {
> > > >            Container = container;
> > > >            RegionManager = Container.Resolve<IRegionManager>();
> > > >            TheEventAggregator = Container.Resolve<IEventAggregator>
> > > > ();
> > > >        }
>
> > > >        public IUnityContainer Container { get; private set; }
> > > >        public IRegionManager RegionManager { get; private set; }
> > > >        protected IEventAggregator TheEventAggregator { get; private
> > > > set; }
>
> > > >        public void
> > > > RegisterViewsAndServicesNotLoadedThroughConfiguration() { }
>
> > > >        public void RaiseAnyPertinentEventsEtc() { }
>
> > > >        public string ConfigurationContainerName { get { return
> > > > "Status"; } }
>
> > > >        public void Initialize()
> > > >        {
> > > >            var section = (UnityConfigurationSection)
> > > > ConfigurationManager.GetSection("Unity");
> > > >            if (section != null && section.Containers
> > > > [ConfigurationContainerName] != null)
> > > >                section.Containers
> > > > [ConfigurationContainerName].Configure(Container);
>
> > > >            RegisterViewsAndServicesNotLoadedThroughConfiguration();
> > > >            RegisterViewsWithRegions();
> > > >            RaiseAnyPertinentEventsEtc();
> > > >        }
>
> > > >        public void RegisterViewsWithRegions()
> > > >        {
> > > >            RegionManager.RegisterViewWithRegion
> > > > (RegionNames.StatusBarRegion, () =>
> > > > Container.Resolve<IStatusBarViewPresenter>().View);
> > > >        }
>
> > > >    }
>
> > > > And this is the full test:
>
> > > >        [Test]
> > > >        public void
> > > > WhenInstantiated_ShouldRegisterIStatusBarViewWithRegionManager()
> > > >        {
> > > >            var unity = MockRepository.GenerateStub<IUnityContainer>
> > > > ();
> > > >            var presenter =
> > > > MockRepository.GenerateStub<IStatusBarViewPresenter>();
> > > >            var view = MockRepository.GenerateStub<IStatusBarView>();
> > > >            presenter.View = view;
>
> > > >            var eventAggregator =
> > > > MockRepository.GenerateStub<IEventAggregator>();
> > > >            var regionManager =
> > > > MockRepository.GenerateStub<IRegionManager>();
> > > >            var region = MockRepository.GenerateStub<IRegion>();
> > > >            var regionCollection =
> > > > MockRepository.GenerateMock<IRegionCollection>();
> > > >            regionCollection.Add(RegionNames.StatusBarRegion, region);
>
> > > >            regionManager.Stub(r => r.Regions).Return
> > > > (regionCollection);
>
> > > >            unity.Stub(container => container.Resolve<IEventAggregator>
> > > > ()).Return(eventAggregator);
> > > >            unity.Stub(container => container.Resolve<IRegionManager>
> > > > ()).Return(regionManager);
> > > >            unity.Stub(container =>
> > > > container.Resolve<IStatusBarViewPresenter>()).Return(presenter);
>
> > > >            new StatusBarModule(unity).Initialize(); //
> > > > RegionManager.Regions doesn't contain any regions!!
>
> > > >            unity.AssertWasCalled(container =>
> > > > container.Resolve<IEventAggregator>());
> > > >            unity.AssertWasCalled(container =>
> > > > container.Resolve<IRegionManager>());
> > > >            unity.AssertWasCalled(container =>
> > > > container.Resolve<IStatusBarViewPresenter>());
> > > >            regionManager.AssertWasCalled(manager =>
> > > > manager.RegisterViewWithRegion(RegionNames.StatusBarRegion, () =>
> > > > presenter.View));
> > > >        }
>
> > > > As you can see, the actual line fo code that I am testing is
>
> > > >            RegionManager.RegisterViewWithRegion
> > > > (RegionNames.StatusBarRegion, () =>
> > > > Container.Resolve<IStatusBarViewPresenter>().View);
>
> > > > Though now having described the whole scenario, I'm thinking that
> > > > really, what I am testing is whether or not the
> > > > "RegisterViewsWithRegions()" member is called; however without
> > > > stubbing the other required operands I cannot see how I would go about
> > > > defining that test.- Hide quoted text -
>
> > > - Show quoted text -- 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to