Testing a Caliburn Micro bootstrapper

I like to have some tests that ensure I can get everything out of the container that I expect to need, before I fire up the application. (Although others would disagree). I mainly have tests for the composition root(s), i.e. anything I am going to try and resolve at runtime.

When developing a WPF app with Caliburn Micro, the container is owned by a Bootstrapper. Unfortunately, it’s not very testable, as most of the interesting methods are protected (but virtual).

The easiest thing to do is create a Test Specific Subclass, and expose the required methods. And then you will find that the base class attempts to initialize the app in the ctor (unless you can find some way to set a flag to false). Thankfully, that method is virtual too, so you can override it and just keep the bits you need:

    public class AppBootstrapperTests
    {
        private TestBootstrapper bootstrapper;

        [TestFixtureSetUp]
        public void FixtureSetUp()
        {
            this.bootstrapper = new TestBootstrapper();
        }

        [Test]
        public void Shell()
        {
            bootstrapper.GetInstance<IShell>();
        }

        private class TestBootstrapper : AppBootstrapper
        {
            public T GetInstance<T>()
            {
                try
                {
                    return (T)base.GetInstance(typeof(T), string.Empty);
                }
                catch (Exception e)
                {
                    this.WhatDoIHave();
                    throw;
                }
            }

            protected override void StartRuntime()
            {
                this.Configure();
            }

            private void WhatDoIHave()
            {
                foreach (var definition in Container.Catalog.Parts)
                {
                    foreach (var exportDefinition in definition.ExportDefinitions)
                    {
                        Console.WriteLine(exportDefinition);
                    }
                }
            }
        }
    }

UPDATE: Added WhatDoIHave to TestBoostrapper.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s