public void CreatesFactoryMethodForMethodWithOneArgAndMatchingConstructorInTarget()
        {
            FactoryMethodFactory factoryMethodFactory;
            MethodInfo methodInfo;
            IDivineInjector injector;
            IFactoryMethod factoryMethod;
            Type domainObjectType;
            ConstructorInfo expectedConstructor;

            Scenario()
                .Given(factoryMethodFactory = new FactoryMethodFactory())
                .Given(methodInfo = typeof(IDummyFactory).GetMethod("MethodWithSinglePassedArg"))
                .Given(injector = AMock<IDivineInjector>()
                    .WhereMethod(i => i.IsBound(typeof(string))).Returns(false)
                    .Instance)
                .Given(domainObjectType = typeof(DomainObjectWithSingleArgConstructor))
                .Given(expectedConstructor = domainObjectType.GetConstructor(new[] { typeof(string) }))

                .When(factoryMethod = factoryMethodFactory.Create(methodInfo, injector, domainObjectType))

                .Then(factoryMethod.Constructor, Is(AnInstance.SameAs(expectedConstructor)))
                .Then(factoryMethod.Name, Is(AString.EqualTo("MethodWithSinglePassedArg")))
                .Then(factoryMethod.ReturnType, Is(AType.EqualTo(typeof(IDomainObject))))
                .Then(factoryMethod.ReturnImplType, Is(AType.EqualTo(typeof(DomainObjectWithSingleArgConstructor))))
                .Then(factoryMethod.ParameterTypes, Is(AList.InOrder().WithOnlyValues(typeof(string))))
                .Then(factoryMethod.ConstructorArgs, Is(AList.InOrder().WithOnly(
                    APassedConstructorArgDefinition.With().Type(typeof(string)))));
        }
        public void CreatesFactoryMethodForMethodWithAnInjectableDependency()
        {
            FactoryMethodFactory factoryMethodFactory;
            MethodInfo methodInfo;
            IDivineInjector injector;
            IFactoryMethod factoryMethod;
            Type domainObjectType;
            ConstructorInfo expectedConstructor;

            Scenario()
                .Given(factoryMethodFactory = new FactoryMethodFactory())
                .Given(methodInfo = typeof(IDummyFactory).GetMethod("MethodWithSingleDependency"))
                .Given(injector = AMock<IDivineInjector>()
                    .WhereMethod(i => i.IsBound(typeof(IDatabase))).Returns(true)
                    .Instance)
                .Given(domainObjectType = typeof(DomainObjectWithOneDependency))
                .Given(expectedConstructor = domainObjectType.GetConstructor(new[] { typeof(IDatabase) }))

                .When(factoryMethod = factoryMethodFactory.Create(methodInfo, injector, domainObjectType))

                .Then(factoryMethod.Constructor, Is(AnInstance.SameAs(expectedConstructor)))
                .Then(factoryMethod.Name, Is(AString.EqualTo("MethodWithSingleDependency")))
                .Then(factoryMethod.ReturnType, Is(AType.EqualTo(typeof(IDomainObject))))
                .Then(factoryMethod.ReturnImplType, Is(AType.EqualTo(typeof(DomainObjectWithOneDependency))))
                .Then(factoryMethod.ParameterTypes, Is(AList.NoItems<Type>()))
                .Then(factoryMethod.ConstructorArgs, Is(AList.InOrder().WithOnly(
                    AnInjectableConstructorArgDefinition.With().Name("Database").PropertyType(typeof(IDatabase)))));
        }
        public void CreatesFactoryMethodForMethodWithNoArgs()
        {
            FactoryMethodFactory factoryMethodFactory;
            MethodInfo methodInfo;
            IDivineInjector injector;
            IFactoryMethod factoryMethod;
            Type domainObjectType;

            Scenario()
                .Given(factoryMethodFactory = new FactoryMethodFactory())
                .Given(methodInfo = typeof(IDummyFactory).GetMethod("MethodWithNoArgs"))
                .Given(injector = AMock<IDivineInjector>().Instance)
                .Given(domainObjectType = typeof(DomainObjectWithDefaultConstructor))

                .When(factoryMethod = factoryMethodFactory.Create(methodInfo, injector, domainObjectType))

                .Then(factoryMethod.Constructor, Is(AnInstance.SameAs(domainObjectType.GetConstructor(new Type[0]))))
                .Then(factoryMethod.Name, Is(AString.EqualTo("MethodWithNoArgs")))
                .Then(factoryMethod.ReturnType, Is(AType.EqualTo(typeof(IDomainObject))))
                .Then(factoryMethod.ReturnImplType, Is(AType.EqualTo(typeof(DomainObjectWithDefaultConstructor))))
                .Then(factoryMethod.ParameterTypes, Is(AList.NoItems<Type>()))
                .Then(factoryMethod.ConstructorArgs, Is(AList.NoItems<IConstructorArgDefinition>()));
        }
        public void CreateThrowsExceptionInCaseNoSuitableConstructorFound()
        {
            FactoryMethodFactory factoryMethodFactory;
            MethodInfo methodInfo;
            IDivineInjector injector;
            Type domainObjectType;
            Exception exception;

            Scenario()
                .Given(factoryMethodFactory = new FactoryMethodFactory())
                .Given(methodInfo = typeof(IDummyFactory).GetMethod("MethodWithSingleDependency"))
                .Given(injector = AMock<IDivineInjector>()
                    .WhereMethod(i => i.IsBound(typeof(IDatabase))).Returns(false)
                    .Instance)
                .Given(domainObjectType = typeof(DomainObjectWithOneDependency))

                .When(exception = CaughtException(() => factoryMethodFactory.Create(methodInfo, injector, domainObjectType)))

                .Then(exception, Is(AnException.With().Message("Could not find constructor on DomainObjectWithOneDependency for factory method IDummyFactory.MethodWithSingleDependency")));
        }