tags:

views:

376

answers:

1

I am constantly facing this error while testing. I cannot even create an instance of IRoleRepository which should return RoleRepository.

Here is the error:

TestCase 'EStudy.Repository.Tests.when_fetching_instance_of_role_repository_using_structuremap.should_fetch_successfully' failed: StructureMap.StructureMapException : StructureMap Exception Code: 202 No Default Instance defined for PluginFamily EStudy.Domain.Model.Model.EStudyModelDataContext, EStudy.Domain.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null at StructureMap.BuildSession.<.ctor>b__0(Type t) at StructureMap.Util.Cache2.get_Item(KEY key) at StructureMap.BuildSession.CreateInstance(Type pluginType) at StructureMap.Pipeline.DefaultInstance.build(Type pluginType, BuildSession session) at StructureMap.Pipeline.Instance.createRawObject(Type pluginType, BuildSession session) at StructureMap.Pipeline.Instance.Build(Type pluginType, BuildSession session) at StructureMap.Pipeline.ConfiguredInstanceBase1.getChild(String propertyName, Type pluginType, BuildSession buildSession) at StructureMap.Pipeline.ConfiguredInstanceBase1.StructureMap.Pipeline.IConfiguredInstance.GetChild(String propertyName, Type pluginType, BuildSession buildSession) at EStudyDomainModelRepositoriesRoleRepositoryInstanceBuilder4c713d65fe464f4792cd90352224a54b.BuildInstance(IConfiguredInstance , BuildSession ) at StructureMap.Pipeline.ConfiguredInstanceBase1.StructureMap.Pipeline.IConfiguredInstance.Build(Type pluginType, BuildSession session, InstanceBuilder builder) at StructureMap.Pipeline.ConfiguredInstanceBase1.build(Type pluginType, BuildSession session) at StructureMap.Pipeline.Instance.createRawObject(Type pluginType, BuildSession session) at StructureMap.Pipeline.Instance.Build(Type pluginType, BuildSession session) at StructureMap.Pipeline.BuildPolicy.Build(BuildSession buildSession, Type pluginType, Instance instance) at StructureMap.InstanceFactory.Build(BuildSession session, Instance instance) at StructureMap.BuildSession.CreateInstance(Type pluginType, Instance instance) at StructureMap.BuildSession.<.ctor>b__0(Type t) at StructureMap.Util.Cache2.get_Item(KEY key) at StructureMap.BuildSession.CreateInstance(Type pluginType) at StructureMap.Container.GetInstance(Type pluginType) at StructureMap.Container.GetInstanceT at StructureMap.ObjectFactory.GetInstancePLUGINTYPE C:\Projects\EStudyVS2008\EStudySoltution\EStudy.Repository.Tests\Test_StructureMapConfiguration.cs(41,0): at EStudy.Repository.Tests.when_fetching_instance_of_role_repository_using_structuremap.should_fetch_successfully()

 [TestFixture]
    public class when_fetching_instance_of_estudy_data_context_using_structuremap : BaseTest
    {

        [Test]
        public void should_fetch_successfully()
        {
           Assert.IsNotNull(ObjectFactory.GetInstance<EStudyModelDataContext>());
        }
    }

BaseTest: 

 public class BaseTest
    {
        private TransactionScope _scope;

        static BaseTest()
        {
            EStudy.Domain.Model.Configuration.StructureMapConfiguration.Configure(); 
        }

And here is the StructureMapConfiguration.Configure method; 

 public class StructureMapConfiguration
    {
        public static void Configure()
        { 
            ObjectFactory.Initialize(InitializeStructureMap);

            ObjectFactory.Initialize(j =>
            {
                j.ForRequestedType<EStudyModelDataContext>().TheDefault.Is.OfConcreteType<EStudyModelDataContext>().WithCtorArg("connection")
                    .EqualTo(ConfigurationManager.ConnectionStrings["EStudyTestDatabaseEntities"].ConnectionString);

            });
        }

        public static void InitializeStructureMap(IInitializationExpression x)
        {
            x.Scan(y =>
                       {
                           y.Assembly("EStudySoltution.EStudy.Domain.Model");
                           y.With<DefaultConventionScanner>();
                       }

                );





        }
+1  A: 

You should not be calling Initialize() more than once. From the docs:

"Any successive calls to Initialize() will effectively wipe out any existing configuration and effectively restart the Container."

If you want to add additional configuration to an existing container, use Objectfactory.Configure().

I'm not sure that explains why you are getting the error you see for EStudyModelDataContext, but it definitely looks at least one problem with what you posted.

You can also see what you do and don't have configured properly by calling container.WhatDoIHave() (which outputs a string you can send to the console).

Phil Sandler