Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Within StructureMap a registry is used to generate a <code>PluginGraph</code>; so to unit test a registry you need to check that it's design produces the correct graph. Unfortunately test verification is best done against an internal property, here's a sample:</p> <pre><code>public interface IFoo {} public class SomeFoo : IFoo {} public class FooRegistry : Registry { public FooRegistry() { For&lt;IFoo&gt;().Use&lt;SomeFoo&gt;(); } } [TestFixture] public class FooRegistryTests { [Test] public void ForIFoo_UseSomeFoo_AsDefaultInstance() { // Arrange var registry = new FooRegistry(); // Act var pluginGraph = registry.Build(); var iFooPluginFamily = pluginGraph.FindFamily(typeof(IFoo)); var defaultInstance = iFooPluginFamily.GetDefaultInstance(); // Assert Assert.IsTrue(defaultInstance.UsesConcreteType&lt;SomeFoo&gt;()); } } public static class TestExtensions { public static bool UsesConcreteType&lt;T&gt;(this Instance instance) { var concreteTypeProperty = typeof (Instance).GetProperty("ConcreteType", BindingFlags.Instance | BindingFlags.NonPublic); if (concreteTypeProperty == null || concreteTypeProperty.PropertyType != typeof(Type)) { Assert.Inconclusive("Unable to locate the internal StructureMap.Instance.ConcreteType property"); } var propertyValue = concreteTypeProperty.GetValue(instance, new object[] {}) as Type; return typeof (T) == propertyValue; } } </code></pre> <p>Testing against an internal property is never desirable, but in the case of testing registry's it's been the best approach I've found. The extension method tries to be just smart enough to be able to make tests which rely on it inconclusive if the internal API changes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload