public void IncompatibleTypes()
        {
            MockBuilderContext ctx = new MockBuilderContext();
            TypeMappingStrategy strategy = new TypeMappingStrategy();
            ctx.Policies.Set<ITypeMappingPolicy>(new TypeMappingPolicy(typeof(object), null), typeof(IFoo), "sales");
            ctx.InnerChain.Add(strategy);

            strategy.BuildUp<IFoo>(ctx, null, "sales");
        }
		public void CanResolveDependencyByTypeAndName()
		{
			MockBuilderContext context = new MockBuilderContext();
			object obj = new object();
			context.Locator.Add(new DependencyResolutionLocatorKey(typeof(object), "Foo"), obj);
			DependencyResolver resolver = new DependencyResolver(context);

			Assert.AreSame(obj, resolver.Resolve(typeof(object), null, "Foo", NotPresentBehavior.ReturnNull, SearchMode.Local));
		}
        public void ConstructorPolicyCanUseLookupToFindAnObject()
        {
            MockBuilderContext ctx = new MockBuilderContext();
            ctx.InnerLocator.Add("foo", 11);

            LookupParameter param = new LookupParameter("foo");

            Assert.AreEqual(11, param.GetValue(ctx));
            Assert.AreSame(typeof (int), param.GetParameterType(ctx));
        }
        private static MockBuilderContext BuildContext()
        {
            MockBuilderContext ctx = new MockBuilderContext();

            ctx.InnerChain.Add(new SingletonStrategy());
            ctx.InnerChain.Add(new CreationStrategy());

            ctx.Policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());

            return ctx;
        }
        public void CreationParameterCanCreateObjectsOfAGivenID()
        {
            MockBuilderContext ctx = new MockBuilderContext();
            NullStrategy strategy = new NullStrategy();
            ctx.InnerChain.Add(strategy);

            CreationParameter param = new CreationParameter(typeof (object), "foo");
            param.GetValue(ctx);

            Assert.AreEqual("foo", strategy.IDRequested);
        }
        public void CreationParameterUsesStrategyChainToCreateObjects()
        {
            MockBuilderContext ctx = new MockBuilderContext();
            NullStrategy strategy = new NullStrategy();
            ctx.InnerChain.Add(strategy);

            CreationParameter param = new CreationParameter(typeof (object));
            param.GetValue(ctx);

            Assert.IsTrue(strategy.WasCalled);
            Assert.AreEqual(typeof (object), strategy.TypeRequested);
        }
		public void ThrowsIfPropertyInjectedIsReadOnly()
		{
			MockBuilderContext ctx = new MockBuilderContext();
			PropertySetterStrategy strategy = new PropertySetterStrategy();

			PropertySetterPolicy policy1 = new PropertySetterPolicy();
			policy1.Properties.Add("Bar", new PropertySetterInfo("Bar", new ValueParameter<string>("value for foo")));
			ctx.Policies.Set<IPropertySetterPolicy>(policy1, typeof(MockInjectionTarget), null);

			MockInjectionTarget target = new MockInjectionTarget();
			strategy.BuildUp<object>(ctx, target, null);
		}
        public void IncompatibleTypesThrow()
        {
            MockBuilderContext ctx = new MockBuilderContext();
            PropertySetterStrategy strategy = new PropertySetterStrategy();

            PropertySetterPolicy policy = new PropertySetterPolicy();
            policy.Properties.Add("Foo", new PropertySetterInfo("Foo", new ValueParameter<object>(new object())));
            ctx.Policies.Set<IPropertySetterPolicy>(policy, typeof(MockInjectionTarget), null);

            MockInjectionTarget target = new MockInjectionTarget();
            strategy.BuildUp<MockInjectionTarget>(ctx, target, null);
        }
        public void BuildChecksConcreteTypeAndNotRequestedType()
        {
            BuilderAwareStrategy strategy = new BuilderAwareStrategy();
            MockBuilderContext context = new MockBuilderContext();
            AwareObject obj = new AwareObject();

            context.InnerChain.Add(strategy);

            context.HeadOfChain.BuildUp(context, typeof(IgnorantObject), obj, null);

            Assert.IsTrue(obj.OnAssembledCalled);
            Assert.IsFalse(obj.OnDisassemblingCalled);
        }
        public void UnbuildCallsClassWithInterface()
        {
            BuilderAwareStrategy strategy = new BuilderAwareStrategy();
            MockBuilderContext context = new MockBuilderContext();
            AwareObject obj = new AwareObject();

            context.InnerChain.Add(strategy);

            context.HeadOfChain.TearDown(context, obj);

            Assert.IsFalse(obj.OnAssembledCalled);
            Assert.IsTrue(obj.OnDisassemblingCalled);
        }
        public void BuildIgnoresClassWithoutInterface()
        {
            BuilderAwareStrategy strategy = new BuilderAwareStrategy();
            MockBuilderContext context = new MockBuilderContext();
            IgnorantObject obj = new IgnorantObject();

            context.InnerChain.Add(strategy);

            context.HeadOfChain.BuildUp(context, typeof(IgnorantObject), obj, null);

            Assert.IsFalse(obj.OnAssembledCalled);
            Assert.IsFalse(obj.OnDisassemblingCalled);
        }
        public void BuildCallsClassWithInterface()
        {
            BuilderAwareStrategy strategy = new BuilderAwareStrategy();
            MockBuilderContext context = new MockBuilderContext();
            AwareObject obj = new AwareObject();

            context.InnerChain.Add(strategy);

            context.HeadOfChain.BuildUp(context, typeof(AwareObject), obj, "foo");

            Assert.IsTrue(obj.OnAssembledCalled);
            Assert.IsFalse(obj.OnDisassemblingCalled);
            Assert.AreEqual("foo", obj.AssembledID);
        }
		public void CloneParametersReturnsNewObjectOfCorrectType()
		{
			MockBuilderContext ctx = new MockBuilderContext();
			ctx.InnerLocator.Add("foo", new CloneableObject());

			CloneParameter cloneParam = new CloneParameter(new LookupParameter("foo"));

			object result1 = cloneParam.GetValue(ctx);
			object result2 = cloneParam.GetValue(ctx);

			Assert.IsTrue(result1 is CloneableObject);
			Assert.IsTrue(result2 is CloneableObject);
			Assert.IsFalse(result1 == result2);
		}
		public void InjectionIsBasedOnConcreteTypeNotRequestedType()
		{
			MockBuilderContext ctx = new MockBuilderContext();
			PropertySetterStrategy strategy = new PropertySetterStrategy();

			PropertySetterPolicy policy1 = new PropertySetterPolicy();
			policy1.Properties.Add("Foo", new PropertySetterInfo("Foo", new ValueParameter<string>("value for foo")));
			ctx.Policies.Set<IPropertySetterPolicy>(policy1, typeof (MockInjectionTarget), null);

			MockInjectionTarget target = new MockInjectionTarget();
			strategy.BuildUp<object>(ctx, target, null);

			Assert.AreEqual("value for foo", target.Foo);
		}
        public void IncompatibleTypesThrows()
        {
            MethodExecutionStrategy strategy = new MethodExecutionStrategy();
            MockBuilderContext ctx = new MockBuilderContext();
            MockObject obj = new MockObject();
            ctx.InnerChain.Add(strategy);

            MethodPolicy policy = new MethodPolicy();
            MethodInfo mi = typeof(MockObject).GetMethod("IntMethod");
            policy.Methods.Add("IntMethod", new MethodCallInfo( mi, new ValueParameter<string>(String.Empty)));
            ctx.Policies.Set<IMethodPolicy>(policy, typeof(MockObject), null);

            ctx.HeadOfChain.BuildUp(ctx, typeof(MockObject), obj, null);
        }
		public void StrategyDoesWorkBasedOnConcreteTypeInsteadOfPassedType()
		{
			MethodExecutionStrategy strategy = new MethodExecutionStrategy();
			MockBuilderContext ctx = new MockBuilderContext();
			MockObject obj = new MockObject();
			ctx.InnerChain.Add(strategy);

			MethodPolicy policy = new MethodPolicy();
			policy.Methods.Add("ParameterlessMethod", new MethodCallInfo("ParameterlessMethod"));
			ctx.Policies.Set<IMethodPolicy>(policy, typeof(MockObject), null);

			ctx.HeadOfChain.BuildUp(ctx, typeof (object), obj, null);

			Assert.IsTrue(obj.ParameterlessWasCalled);
		}
		public void StrategyCallsMethodWithDirectValues()
		{
			MethodExecutionStrategy strategy = new MethodExecutionStrategy();
			MockBuilderContext ctx = new MockBuilderContext();
			MockObject obj = new MockObject();
			ctx.InnerChain.Add(strategy);

			MethodPolicy policy = new MethodPolicy();
			policy.Methods.Add("IntMethod", new MethodCallInfo("IntMethod", 32));
			ctx.Policies.Set<IMethodPolicy>(policy, typeof (MockObject), null);

			ctx.HeadOfChain.BuildUp(ctx, typeof (MockObject), obj, null);

			Assert.AreEqual(32, obj.IntValue);
		}
        public void CanCallMultiParameterMethods()
        {
            MethodExecutionStrategy strategy = new MethodExecutionStrategy();
            MockBuilderContext ctx = new MockBuilderContext();
            MockObject obj = new MockObject();
            ctx.InnerChain.Add(strategy);

            MethodPolicy policy = new MethodPolicy();
            policy.Methods.Add("MultiParamMethod", new MethodCallInfo("MultiParamMethod", 1.0, "foo"));
            ctx.Policies.Set<IMethodPolicy>(policy, typeof (MockObject), null);

            ctx.HeadOfChain.BuildUp(ctx, typeof (MockObject), obj, null);

            Assert.AreEqual(1.0, obj.MultiDouble);
            Assert.AreEqual("foo", obj.MultiString);
        }
		public void ResolverUsesNamesToResolveDifferences()
		{
			MockBuilderContext context = new MockBuilderContext();
			DependencyResolver resolver = new DependencyResolver(context);

			object obj1 = new object();
			context.Locator.Add(new DependencyResolutionLocatorKey(typeof(object), null), obj1);

			object obj2 = new object();
			context.Locator.Add(new DependencyResolutionLocatorKey(typeof(object), "Foo"), obj2);

			object obj3 = new object();
			context.Locator.Add(new DependencyResolutionLocatorKey(typeof(object), "Bar"), obj3);

			Assert.AreSame(obj1, resolver.Resolve(typeof(object), null, null, NotPresentBehavior.ReturnNull, SearchMode.Local));
			Assert.AreSame(obj2, resolver.Resolve(typeof(object), null, "Foo", NotPresentBehavior.ReturnNull, SearchMode.Local));
			Assert.AreSame(obj3, resolver.Resolve(typeof(object), null, "Bar", NotPresentBehavior.ReturnNull, SearchMode.Local));
		}
        public void CanMapInterfacesToConcreteTypes()
        {
            MockBuilderContext ctx = new MockBuilderContext();
            TypeMappingStrategy strategy = new TypeMappingStrategy();
            ctx.Policies.Set<ITypeMappingPolicy>(new TypeMappingPolicy(typeof(SalesFoo), null), typeof(IFoo), "sales");
            ctx.Policies.Set<ITypeMappingPolicy>(new TypeMappingPolicy(typeof(Foo), null), typeof(IFoo), "marketing");
            ctx.InnerChain.Add(strategy);

            MockStrategy mock = new MockStrategy();
            ctx.InnerChain.Add(mock);

            strategy.BuildUp<IFoo>(ctx, null, "sales");

            Assert.IsTrue(mock.WasRun);
            Assert.AreEqual(typeof(SalesFoo), mock.IncomingType);

            mock.WasRun = false;
            mock.IncomingType = null;

            strategy.BuildUp<IFoo>(ctx, null, "marketing");

            Assert.IsTrue(mock.WasRun);
            Assert.AreEqual(typeof(Foo), mock.IncomingType);
        }
        public void StrategyWithNoObjectDoesntThrow()
        {
            MethodExecutionStrategy strategy = new MethodExecutionStrategy();
            MockBuilderContext ctx = new MockBuilderContext();
            ctx.InnerChain.Add(strategy);

            ctx.HeadOfChain.BuildUp(ctx, typeof (object), null, null);
        }
        public void StrategyDoesNothingWithNoPolicy()
        {
            MethodExecutionStrategy strategy = new MethodExecutionStrategy();
            MockBuilderContext ctx = new MockBuilderContext();
            MockObject obj = new MockObject();
            ctx.InnerChain.Add(strategy);

            ctx.HeadOfChain.BuildUp(ctx, typeof (MockObject), obj, null);

            Assert.IsFalse(obj.ParameterlessWasCalled);
        }
        public void StrategyCallsMultipleMethodsAndCallsThemInOrder()
        {
            MethodExecutionStrategy strategy = new MethodExecutionStrategy();
            MockBuilderContext ctx = new MockBuilderContext();
            MockObject obj = new MockObject();
            ctx.InnerChain.Add(strategy);

            MethodPolicy policy = new MethodPolicy();
            policy.Methods.Add("ParameterlessMethod", new MethodCallInfo("ParameterlessMethod"));
            policy.Methods.Add("IntMethod", new MethodCallInfo("IntMethod", new ValueParameter<int>(32)));
            ctx.Policies.Set<IMethodPolicy>(policy, typeof (MockObject), null);

            ctx.HeadOfChain.BuildUp(ctx, typeof (MockObject), obj, null);

            Assert.AreEqual(1, obj.CallOrderParameterless);
            Assert.AreEqual(2, obj.CallOrderInt);
        }
		private MockBuilderContext CreateContext()
		{
			MockBuilderContext result = new MockBuilderContext();
			result.InnerChain.Add(new CreationStrategy());
			result.Policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());
			return result;
		}
        public void CreationStrategyWithNoPoliciesFails()
        {
            MockBuilderContext ctx = CreateContext();

            ctx.HeadOfChain.BuildUp(ctx, typeof(object), null, null);
        }
        public void SettingPolicyForMissingMethodDoesntThrow()
        {
            MethodExecutionStrategy strategy = new MethodExecutionStrategy();
            MockBuilderContext ctx = new MockBuilderContext();
            MockObject obj = new MockObject();
            ctx.InnerChain.Add(strategy);

            MethodPolicy policy = new MethodPolicy();
            policy.Methods.Add("NonExistantMethod", new MethodCallInfo("NonExistantMethod"));
            ctx.Policies.Set<IMethodPolicy>(policy, typeof (MockObject), null);

            ctx.HeadOfChain.BuildUp(ctx, typeof (MockObject), obj, null);
        }
        public void CanCreateValueTypesWithConstructorInjectionStrategyInPlace()
        {
            MockBuilderContext context = CreateContext();

            Assert.AreEqual(0, context.HeadOfChain.BuildUp(context, typeof(int), null, null));
        }
        public void SpecifyingMultipleConstructorsThrows()
        {
            MockBuilderContext context = CreateContext();

            context.HeadOfChain.BuildUp(context, typeof(MockInvalidDualConstructorAttributes), null, null);
        }
 private MockBuilderContext CreateContext()
 {
     MockBuilderContext result = new MockBuilderContext();
     result.InnerChain.Add(new SingletonStrategy());
     result.InnerChain.Add(new CreationStrategy());
     return result;
 }
 private MockBuilderContext CreateContext(IReadWriteLocator locator)
 {
     MockBuilderContext result = new MockBuilderContext(locator);
     result.InnerChain.Add(new SingletonStrategy());
     result.InnerChain.Add(new MethodReflectionStrategy());
     result.InnerChain.Add(new CreationStrategy());
     result.InnerChain.Add(new MethodExecutionStrategy());
     result.Policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());
     result.Policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));
     return result;
 }
        public void SpecifyingCreateNewAndDependencyThrows()
        {
            MockBuilderContext context = CreateContext();

            context.HeadOfChain.BuildUp(context, typeof(MockInvalidDualParameterAttributes), null, null);
        }
Beispiel #32
0
        public void ThrowsIfConcreteTypeToCreateCannotBeCreated()
        {
            MockBuilderContext context = CreateContext();

            context.HeadOfChain.BuildUp(context, typeof(MockDependsOnInterface), null, null);
        }
        public void StrategyThrowsIfNamedObjectNotPresent()
        {
            MockBuilderContext context = CreateContext();

            context.HeadOfChain.BuildUp(context, typeof(NamedThrowingMockObject), null, null);
        }
        public void SettingPolicyForWrongParameterCountDoesntThrow()
        {
            MethodExecutionStrategy strategy = new MethodExecutionStrategy();
            MockBuilderContext ctx = new MockBuilderContext();
            MockObject obj = new MockObject();
            ctx.InnerChain.Add(strategy);

            MethodPolicy policy = new MethodPolicy();
            policy.Methods.Add("ParameterlessMethod", new MethodCallInfo("ParameterlessMethod", 123));
            ctx.Policies.Set<IMethodPolicy>(policy, typeof (MockObject), null);

            ctx.HeadOfChain.BuildUp(ctx, typeof (MockObject), obj, null);
        }
		public void ReturnsNullWhenPassedNull()
		{
			MockBuilderContext ctx = new MockBuilderContext();
			PropertySetterStrategy strategy = new PropertySetterStrategy();
			Assert.IsNull(strategy.BuildUp<object>(ctx, null, null));
		}