public void should_be_able_to_resolve_from_the_generic_family_expression()
        {
            var widget = new AWidget();
            var container = new Container(x => x.For(typeof (IWidget)).Use(widget).Named("mine"));

            container.GetInstance<IWidget>("mine").ShouldBeTheSameAs(widget);
        }
        public void add_an_instance_by_literal_object()
        {
            var aWidget = new AWidget();

            var container = new Container(x => { x.For<IWidget>().Use(aWidget); });

            container.GetAllInstances<IWidget>().First().ShouldBeTheSameAs(aWidget);
        }
Esempio n. 3
0
        public void get_uses_the_family_to_return()
        {
            var widget = new AWidget();

            family.Stub(x => x.Build(instance)).Return(widget);

            instanceRef.Get<IWidget>().ShouldBeTheSameAs(widget);
        }
        public void get_for_uncached_instance_returns_passed_instance()
        {
            var aWidget = new AWidget();
            var instance = new ObjectInstance(aWidget);

            var cachedWidget = cache.Get(typeof (IWidget), instance, new StubBuildSession());

            cachedWidget.ShouldBe(aWidget);
        }
        public void get_for_cached_instance_returns_cached_instance()
        {
            var aWidget = new AWidget();
            var instance = new ObjectInstance(aWidget);
            cache.Get(typeof(IWidget), instance, new StubBuildSession());
            
            var cachedWidget = cache.Get(typeof(IWidget), instance, new StubBuildSession());

            Assert.AreEqual(aWidget, cachedWidget);
        }     
        public void eject_a_non_disposable_object()
        {
            var widget = new AWidget();
            var instance = new ObjectInstance(widget);

            cache.Set(typeof (IWidget), instance, widget);

            cache.Eject(typeof (IWidget), instance);

            cache.Has(typeof (IWidget), instance).ShouldBeFalse();
        }
        public void Add_default_instance_with_literal()
        {
            var registry  = new Registry();
            var theWidget = new AWidget();

            var theProfileName = "something";

            registry.Profile(theProfileName, p => { p.For <IWidget>().Use(theWidget); });


            var graph = registry.Build();

            graph.Profile("something").Families[typeof(IWidget)].GetDefaultInstance()
            .ShouldBeOfType <ObjectInstance <AWidget, IWidget> >()
            .Object.ShouldBeTheSameAs(theWidget);
        }
        public void use_all_instances_of_an_enumerable_element_type()
        {
            var widget1 = new AWidget();
            var widget2 = new AWidget();
            var widget3 = new AWidget();

            var container = new Container(x => {
                x.For<IWidget>().AddInstances(o => {
                    o.Object(widget1);
                    o.Object(widget2);
                    o.Object(widget3);
                });
            });

            container.GetInstance<ClassWithWidgets>().Widgets.ShouldHaveTheSameElementsAs(widget1, widget2, widget3);
        }
        public void get_for_cached_instance_created_on_different_thread_returns_cached_instance()
        {
            var aWidget = new AWidget();
            var instance = new ObjectInstance(aWidget);
            cache.Get(typeof (IWidget), instance, new StubBuildSession());

            object cachedWidget = null;
            var thread =
                new Thread(() => { cachedWidget = cache.Get(typeof (IWidget), instance, new StubBuildSession()); });

            thread.Start();
            // Allow 10ms for the thread to start and for Get call to complete
            thread.Join(10);

            cachedWidget.ShouldNotBeNull();
            cachedWidget.ShouldBe(aWidget);
        }
        public void has()
        {
            var widget = new AWidget();
            var instance = new ObjectInstance(widget);

            cache.Has(typeof (IWidget), instance).ShouldBeFalse();

            cache.Set(typeof (Rule), instance, widget);

            cache.Has(typeof (IWidget), instance).ShouldBeFalse();

            cache.Set(typeof (IWidget), new ObjectInstance(new AWidget()), widget);

            cache.Has(typeof (IWidget), instance).ShouldBeFalse();

            cache.Set(typeof (IWidget), instance, widget);

            cache.Has(typeof (IWidget), instance).ShouldBeTrue();
        }
        public void get_for_cached_instance_created_on_different_thread_returns_cached_instance()
        {
            var aWidget = new AWidget();
            var instance = new ObjectInstance(aWidget);
            cache.Get(typeof(IWidget), instance, new StubBuildSession());
            
            object cachedWidget = null;
            var thread = new Thread(() =>
            {
                cachedWidget = cache.Get(typeof(IWidget), instance, new StubBuildSession());
            });

            thread.Start();
            // Allow 10ms for the thread to start and for Get call to complete
            thread.Join(10);

            Assert.NotNull(cachedWidget, "Get did not return cachedWidget within allowed time. Is your thread being blocked?");
            Assert.AreEqual(aWidget, cachedWidget);
        }   
        public void Set_the_default_to_a_built_object()
        {
            var aWidget = new AWidget();

            var manager =
                new Container(
                    registry => registry.ForRequestedType<IWidget>().TheDefault.Is.Object(aWidget));

            Assert.AreSame(aWidget, manager.GetInstance<IWidget>());
        }
        public void Set_the_default_to_a_built_object()
        {
            var aWidget = new AWidget();

            var manager =
                new Container(
                    registry => registry.For<IWidget>().Use(aWidget));

            aWidget.ShouldBeTheSameAs(manager.GetInstance<IWidget>());
        }
        public void get_and_set_a_non_primitive_type_that_is_not_enumerable()
        {
            ConstructorInstance instance = ConstructorInstance.For<ClassWithNonPrimitive>();

            var widget = new AWidget();

            instance.As<IConfiguredInstance>().SetValue("widget", widget);

            instance.Get("widget", typeof (IWidget), new StubBuildSession()).ShouldEqual(widget);
        }
        public void Add_default_instance_with_literal()
        {
            var registry = new Registry();
            var theWidget = new AWidget();

            string theProfileName = "something";
            registry.Profile(theProfileName, p => {
                p.For<IWidget>().Use(theWidget);
            });

            PluginGraph graph = registry.Build();
            graph.Profile("something").Families[typeof (IWidget)].GetDefaultInstance()
                                                                 .ShouldBeOfType<ObjectInstance>()
                                                                 .Object.ShouldBeTheSameAs(theWidget);
        }
        public void Add_default_instance_with_literal()
        {
            var registry = new Registry();
            var theWidget = new AWidget();

            string theProfileName = "something";
            registry.Profile(theProfileName)
                .For<IWidget>().Use(theWidget);

            PluginGraph graph = registry.Build();
            var instance = (ObjectInstance) graph.ProfileManager.GetDefault(typeof (IWidget), "something");

            Assert.AreSame(theWidget, instance.Object);
        }