private void AssertThatProxyWasGeneratedWhenArgumentsForConstructorAreSpecified(Type typeOfFake, ProxyGeneratorResult result, FakeOptions fakeOptions)
 {
     if (!result.ProxyWasSuccessfullyGenerated && fakeOptions.ArgumentsForConstructor != null)
     {
         this.thrower.ThrowFailedToGenerateProxyWithArgumentsForConstructor(typeOfFake, result.ReasonForFailure);
     }
 }
        public object CreateFake(Type typeOfFake, FakeOptions options)
        {
            var result = this.fakeCreator.CreateFake(typeOfFake, options, this.session, throwOnFailure: true);

            this.ApplyConfigurationFromOptions(result, options);

            return result;
        }
示例#3
0
        private ProxyGeneratorResult GenerateProxy(Type typeOfFake, FakeOptions fakeOptions, IEnumerable <object> argumentsForConstructor)
        {
            var fakeCallProcessorProvider = this.fakeCallProcessorProviderFactory(typeOfFake);

            return(this.proxyGenerator.GenerateProxy(
                       typeOfFake,
                       fakeOptions.AdditionalInterfacesToImplement,
                       argumentsForConstructor,
                       fakeOptions.AdditionalAttributes,
                       fakeCallProcessorProvider));
        }
        public bool TryCreateFake(Type typeOfFake, FakeOptions options, out object result)
        {
            result = this.fakeCreator.CreateFake(typeOfFake, options, this.session, throwOnFailure: false);

            if (result == null)
            {
                return(false);
            }

            this.ApplyConfigurationFromOptions(result, options);
            return(true);
        }
        private void ApplyConfigurationFromOptions(object fake, FakeOptions options)
        {
            if (options.WrappedInstance != null)
            {
                this.wrapperConfigurer.ConfigureFakeToWrap(fake, options.WrappedInstance, options.SelfInitializedFakeRecorder);
            }

            foreach (var a in options.OnFakeCreatedActions)
            {
                a.Invoke(fake);
            }
        }
        public bool TryCreateFake(Type typeOfFake, FakeOptions options, out object result)
        {
            result = this.fakeCreator.CreateFake(typeOfFake, options, this.session, throwOnFailure: false);

            if (result == null)
            {
                return false;
            }

            this.ApplyConfigurationFromOptions(result, options);
            return true;
        }
        private void ApplyConfigurationFromOptions(object fake, FakeOptions options)
        {
            if (options.WrappedInstance != null)
            {
                this.wrapperConfigurer.ConfigureFakeToWrap(fake, options.WrappedInstance, options.SelfInitializedFakeRecorder);
            }

            foreach (var a in options.OnFakeCreatedActions)
            {
                a.Invoke(fake);
            }
        }
        public object CreateFake(Type typeOfFake, FakeOptions fakeOptions, IDummyValueCreationSession session, bool throwOnFailure)
        {
            var result = this.GenerateProxy(typeOfFake, fakeOptions, fakeOptions.ArgumentsForConstructor);

            if (throwOnFailure)
            {
                this.AssertThatProxyWasGeneratedWhenArgumentsForConstructorAreSpecified(typeOfFake, result, fakeOptions);
            }

            if (!result.ProxyWasSuccessfullyGenerated && fakeOptions.ArgumentsForConstructor == null)
            {
                result = this.TryCreateFakeWithDummyArgumentsForConstructor(typeOfFake, fakeOptions, session, result.ReasonForFailure, throwOnFailure);
            }

            return result != null ? result.GeneratedProxy : null;
        }
示例#9
0
        public virtual object CreateFake(Type typeOfFake, FakeOptions fakeOptions, IDummyValueCreationSession session, bool throwOnFailure)
        {
            var result = this.GenerateProxy(typeOfFake, fakeOptions, fakeOptions.ArgumentsForConstructor);

            if (throwOnFailure)
            {
                this.AssertThatProxyWasGeneratedWhenArgumentsForConstructorAreSpecified(typeOfFake, result, fakeOptions);
            }

            if (!result.ProxyWasSuccessfullyGenerated && fakeOptions.ArgumentsForConstructor == null)
            {
                result = this.TryCreateFakeWithDummyArgumentsForConstructor(typeOfFake, fakeOptions, session, result.ReasonForFailure, throwOnFailure);
            }

            return(result != null ? result.GeneratedProxy : null);
        }
        public FakeManagerProvider(
                FakeManager.Factory fakeManagerFactory,
                IFakeManagerAccessor fakeManagerAccessor,
                IFakeObjectConfigurator fakeObjectConfigurator, 
                Type typeOfFake,
                FakeOptions fakeOptions)
        {
            Guard.AgainstNull(fakeManagerFactory, "fakeManagerFactory");
            Guard.AgainstNull(fakeManagerAccessor, "fakeManagerAccessor");
            Guard.AgainstNull(fakeObjectConfigurator, "fakeObjectConfigurator");
            Guard.AgainstNull(typeOfFake, "typeOfFake");
            Guard.AgainstNull(fakeOptions, "fakeOptions");

            this.fakeManagerFactory = fakeManagerFactory;
            this.fakeManagerAccessor = fakeManagerAccessor;
            this.fakeObjectConfigurator = fakeObjectConfigurator;
            this.typeOfFake = typeOfFake;
            this.fakeOptions = fakeOptions;
        }
        public void Should_not_perform_actions_froM_options_when_trying_to_create_fake_but_fails()
        {
            // Arrange
            var wasInvoked = false;

            var options = new FakeOptions();

            options.OnFakeCreatedActions.Add(x => wasInvoked = true);

            A.CallTo(() =>
                this.fakeCreator.CreateFake(A<Type>._, A<FakeOptions>._, A<IDummyValueCreationSession>._, A<bool>._))
                .Returns(null);

            // Act
            this.manager.TryCreateFake(typeof(IFoo), options, out Ignore.This<object>().Value);

            // Assert
            Assert.That(wasInvoked, Is.False);
        }
        public void Should_perform_actions_from_options_on_created_fake_when_creating_fake()
        {
            // Arrange
            object invokedWithFake = null;

            var options = new FakeOptions();

            options.OnFakeCreatedActions.Add(x => invokedWithFake = x);

            var fake = A.Dummy<IFoo>();

            A.CallTo(() =>
                this.fakeCreator.CreateFake(typeof(IFoo), options, this.dummySession, true))
                .Returns(fake);

            // Act
            this.manager.CreateFake(typeof(IFoo), options);

            // Assert
            Assert.That(invokedWithFake, Is.SameAs(fake));
        }
        private ProxyGeneratorResult TryCreateFakeWithDummyArgumentsForConstructor(Type typeOfFake, FakeOptions fakeOptions, IDummyValueCreationSession session, string failReasonForDefaultConstructor, bool throwOnFailure)
        {
            var constructors = ResolveConstructors(typeOfFake, session);

            foreach (var constructor in constructors.Where(x => x.WasSuccessfullyResolved))
            {
                var result = this.GenerateProxy(typeOfFake, fakeOptions, constructor.Arguments.Select(x => x.ResolvedValue));

                if (result.ProxyWasSuccessfullyGenerated)
                {
                    return result;
                }

                constructor.ReasonForFailure = result.ReasonForFailure;
            }

            if (throwOnFailure)
            {
                this.thrower.ThrowFailedToGenerateProxyWithResolvedConstructors(typeOfFake, failReasonForDefaultConstructor, constructors);
            }

            return null;
        }
示例#14
0
        public virtual object CreateFake(Type typeOfFake, FakeOptions fakeOptions, IDummyValueCreationSession session, bool throwOnFailure)
        {
            var result = this.proxyGenerator.GenerateProxy(typeOfFake, fakeOptions.AdditionalInterfacesToImplement, fakeOptions.ArgumentsForConstructor, fakeOptions.AdditionalAttributes);

            if (throwOnFailure)
            {
                this.AssertThatProxyWasGeneratedWhenArgumentsForConstructorAreSpecified(typeOfFake, result, fakeOptions);
            }

            if (!result.ProxyWasSuccessfullyGenerated && fakeOptions.ArgumentsForConstructor == null)
            {
                result = this.TryCreateFakeWithDummyArgumentsForConstructor(typeOfFake, fakeOptions, session, result.ReasonForFailure, throwOnFailure);
            }

            if (result != null)
            {
                this.fakeManagerAttacher.AttachFakeManagerToProxy(typeOfFake, result.GeneratedProxy, result.CallInterceptedEventRaiser);
                this.configurer.ConfigureFake(typeOfFake, result.GeneratedProxy);
                return(result.GeneratedProxy);
            }

            return(null);
        }
示例#15
0
        public virtual object CreateFake(Type typeOfFake, FakeOptions fakeOptions, IDummyValueCreationSession session, bool throwOnFailure)
        {
            var result = this.proxyGenerator.GenerateProxy(typeOfFake, fakeOptions.AdditionalInterfacesToImplement, fakeOptions.ArgumentsForConstructor, fakeOptions.AdditionalAttributes);

            if (throwOnFailure)
            {
                this.AssertThatProxyWasGeneratedWhenArgumentsForConstructorAreSpecified(typeOfFake, result, fakeOptions);
            }

            if (!result.ProxyWasSuccessfullyGenerated && fakeOptions.ArgumentsForConstructor == null)
            {
                result = this.TryCreateFakeWithDummyArgumentsForConstructor(typeOfFake, fakeOptions, session, result.ReasonForFailure, throwOnFailure);
            }

            if (result != null)
            {
                this.fakeManagerAttacher.AttachFakeManagerToProxy(typeOfFake, result.GeneratedProxy, result.CallInterceptedEventRaiser);
                this.configurer.ConfigureFake(typeOfFake, result.GeneratedProxy);
                return result.GeneratedProxy;
            }

            return null;
        }
 public object CreateFake(Type typeOfFake, FakeOptions options)
 {
     return(this.fakeCreator.CreateFake(typeOfFake, options, this.session, throwOnFailure: true));
 }
示例#17
0
        private ProxyGeneratorResult TryCreateFakeWithDummyArgumentsForConstructor(Type typeOfFake, FakeOptions fakeOptions, IDummyValueCreationSession session, string failReasonForDefaultConstructor, bool throwOnFailure)
        {
            var constructors = ResolveConstructors(typeOfFake, session);

            foreach (var constructor in constructors.Where(x => x.WasSuccessfullyResolved))
            {
                Logger.Debug("Trying with constructor with {0} arguments.", constructor.Arguments.Length);

                var result = this.proxyGenerator.GenerateProxy(typeOfFake, fakeOptions.AdditionalInterfacesToImplement, constructor.Arguments.Select(x => x.ResolvedValue), fakeOptions.AdditionalAttributes);

                if (result.ProxyWasSuccessfullyGenerated)
                {
                    return result;
                }
                else
                {
                    Logger.Debug("Setting reason for failure of constructor to {0}.", result.ReasonForFailure);
                    constructor.ReasonForFailure = result.ReasonForFailure;
                }
            }

            if (throwOnFailure)
            {
                this.thrower.ThrowFailedToGenerateProxyWithResolvedConstructors(typeOfFake, failReasonForDefaultConstructor, constructors);
            }

            return null;
        }
示例#18
0
        private ProxyGeneratorResult TryCreateFakeWithDummyArgumentsForConstructor(Type typeOfFake, FakeOptions fakeOptions, IDummyValueCreationSession session, string failReasonForDefaultConstructor, bool throwOnFailure)
        {
            var constructors = ResolveConstructors(typeOfFake, session);

            foreach (var constructor in constructors.Where(x => x.WasSuccessfullyResolved))
            {
                var result = this.GenerateProxy(typeOfFake, fakeOptions, constructor.Arguments.Select(x => x.ResolvedValue));

                if (result.ProxyWasSuccessfullyGenerated)
                {
                    return(result);
                }
                else
                {
                    constructor.ReasonForFailure = result.ReasonForFailure;
                }
            }

            if (throwOnFailure)
            {
                this.thrower.ThrowFailedToGenerateProxyWithResolvedConstructors(typeOfFake, failReasonForDefaultConstructor, constructors);
            }

            return(null);
        }
示例#19
0
 private void AssertThatProxyWasGeneratedWhenArgumentsForConstructorAreSpecified(Type typeOfFake, ProxyGeneratorResult result, FakeOptions fakeOptions)
 {
     if (!result.ProxyWasSuccessfullyGenerated && fakeOptions.ArgumentsForConstructor != null)
     {
         this.thrower.ThrowFailedToGenerateProxyWithArgumentsForConstructor(typeOfFake, result.ReasonForFailure);
     }
 }
 public bool TryCreateFake(Type typeOfFake, FakeOptions options, out object result)
 {
     result = this.fakeCreator.CreateFake(typeOfFake, options, this.session, throwOnFailure: false);
     return result != null;
 }
示例#21
0
        private ProxyGeneratorResult TryCreateFakeWithDummyArgumentsForConstructor(Type typeOfFake, FakeOptions fakeOptions, IDummyValueCreationSession session, string failReasonForDefaultConstructor, bool throwOnFailure)
        {
            var constructors = ResolveConstructors(typeOfFake, session);

            foreach (var constructor in constructors.Where(x => x.WasSuccessfullyResolved))
            {
                logger.Debug("Trying with constructor with {0} arguments.", constructor.Arguments.Length);

                var result = this.proxyGenerator.GenerateProxy(typeOfFake, fakeOptions.AdditionalInterfacesToImplement, constructor.Arguments.Select(x => x.ResolvedValue), fakeOptions.AdditionalAttributes);

                if (result.ProxyWasSuccessfullyGenerated)
                {
                    return(result);
                }
                else
                {
                    logger.Debug("Setting reason for failure of constructor to {0}.", result.ReasonForFailure);
                    constructor.ReasonForFailure = result.ReasonForFailure;
                }
            }

            if (throwOnFailure)
            {
                this.thrower.ThrowFailedToGenerateProxyWithResolvedConstructors(typeOfFake, failReasonForDefaultConstructor, constructors);
            }

            return(null);
        }
 public bool TryCreateFake(Type typeOfFake, FakeOptions options, out object result)
 {
     result = this.fakeCreator.CreateFake(typeOfFake, options, this.session, throwOnFailure: false);
     return(result != null);
 }
 public object CreateFake(Type typeOfFake, FakeOptions options)
 {
     return this.fakeCreator.CreateFake(typeOfFake, options, this.session, throwOnFailure: true);
 }
        private ProxyGeneratorResult GenerateProxy(Type typeOfFake, FakeOptions fakeOptions, IEnumerable<object> argumentsForConstructor)
        {
            var fakeCallProcessorProvider = this.fakeCallProcessorProviderFactory(typeOfFake, fakeOptions);

            return this.proxyGenerator.GenerateProxy(
                    typeOfFake,
                    fakeOptions.AdditionalInterfacesToImplement,
                    argumentsForConstructor,
                    fakeOptions.AdditionalAttributes,
                    fakeCallProcessorProvider);
        }