Ejemplo n.º 1
0
        public void CallsMethodsMarkedWithInjectionAttribute()
        {
            MockBuilderContext context = GetContext();
            var    key         = new NamedTypeBuildKey <ObjectWithInjectionMethod>();
            object intValue    = 42;
            object stringValue = "Hello world";

            SetSingleton(context, typeof(int), intValue);
            SetSingleton(context, typeof(string), stringValue);

            IBuildPlanPolicy plan =
                GetPlanCreator(context).CreatePlan(context, key);

            ObjectWithInjectionMethod existing = new ObjectWithInjectionMethod();

            context.BuildKey = key;
            context.Existing = existing;
            plan.BuildUp(context);

            GC.KeepAlive(intValue);
            GC.KeepAlive(stringValue);

            Assert.IsTrue(existing.WasInjected);
            Assert.AreEqual(intValue, existing.IntValue);
            Assert.AreEqual(stringValue, existing.StringValue);
        }
Ejemplo n.º 2
0
        public void InjectionMethodsAreCalled()
        {
            ObjectWithInjectionMethod obj =
                ResolveConfiguredObject <ObjectWithInjectionMethod>("method");

            Assert.AreEqual(obj.ConnectionString, "contoso");
            Assert.IsInstanceOfType(obj.Logger, typeof(MockLogger));
        }
Ejemplo n.º 3
0
        public void ConfigInject_InjectionMethodWithTwoDifferentParameters()
        {
            ObjectWithInjectionMethod obj =
                ResolveConfiguredObject <ObjectWithInjectionMethod>("methodwithTwoDifferentInjectionParameters");

            Assert.IsInstanceOfType(obj.Service, typeof(IService));
            Assert.IsInstanceOfType(obj.Logger, typeof(SpecialLogger));
        }
Ejemplo n.º 4
0
        public void ConfigInject_InjectionMethodWithTwoSameParameters()
        {
            ObjectWithInjectionMethod obj =
                ResolveConfiguredObject <ObjectWithInjectionMethod>("methodwithTwoSameInjectionParameters");

            Assert.IsInstanceOfType(obj.Logger1, typeof(MockLogger));
            Assert.IsInstanceOfType(obj.Logger2, typeof(SpecialLogger));
        }
Ejemplo n.º 5
0
        public void ConfigInject_CanConfigureOnePropertyAndOneMethod()
        {
            ObjectWithInjectionMethod obj =
                ResolveConfiguredObject <ObjectWithInjectionMethod>(
                    "ObjectWithInjectionMethodContainer");

            Assert.IsNotNull(obj.Service);
            Assert.IsInstanceOfType(obj.Service, typeof(EmailService));
        }
Ejemplo n.º 6
0
        public void GetProperExceptionMessageWhenFailingToResolveMethodParameter()
        {
            TestingBuilderContext context     = TestingBuilderContext.GetFullContext();
            ILifetimePolicy       intLifetime = new SingletonLifetimePolicy();

            intLifetime.SetValue(42);
            context.PersistentPolicies.Set(intLifetime, typeof(int));

            ObjectWithInjectionMethod existing = new ObjectWithInjectionMethod();

            try
            {
                context.ExecuteBuildUp(typeof(ObjectWithInjectionMethod), existing);
                Assert.Fail();
            }
            catch (BuildFailedException ex)
            {
                StringAssert.Contains(ex.Message, "DoSomething");
                StringAssert.Contains(ex.Message, "stringParam");
            }
        }