Example #1
0
            public CreationResult CreateFake(
                Type typeOfFake,
                IProxyOptions proxyOptions,
                IDummyValueResolver resolver,
                LoopDetectingResolutionContext resolutionContext)
            {
                if (!CastleDynamicProxyGenerator.CanGenerateProxy(typeOfFake, out string?reasonCannotGenerate))
                {
                    return(CreationResult.FailedToCreateFake(typeOfFake, reasonCannotGenerate));
                }

                if (proxyOptions.ArgumentsForConstructor is not null)
                {
                    var proxyGeneratorResult = this.GenerateProxy(typeOfFake, proxyOptions, proxyOptions.ArgumentsForConstructor);

                    return(proxyGeneratorResult.ProxyWasSuccessfullyGenerated
                        ? CreationResult.SuccessfullyCreated(proxyGeneratorResult.GeneratedProxy)
                        : CreationResult.FailedToCreateFake(typeOfFake, proxyGeneratorResult.ReasonForFailure !));
                }

                return(this.TryCreateFakeWithDummyArgumentsForConstructor(
                           typeOfFake,
                           proxyOptions,
                           resolver,
                           resolutionContext));
            }
        public void Should_return_proxy(Type typeOfProxy)
        {
            // Arrange

            // Act
            var result = CastleDynamicProxyGenerator.GenerateProxy(typeOfProxy, Enumerable.Empty <Type>(), null, Enumerable.Empty <Expression <Func <Attribute> > >(), A.Dummy <IFakeCallProcessorProvider>());

            // Assert
            result.GeneratedProxy.Should().NotBeNull();
        }
        public void Should_specify_that_no_default_constructor_was_found()
        {
            // Arrange

            // Act
            var result = CastleDynamicProxyGenerator.GenerateProxy(typeof(ClassWithPrivateConstructor), Enumerable.Empty <Type>(), null, Enumerable.Empty <Expression <Func <Attribute> > >(), A.Dummy <IFakeCallProcessorProvider>());

            // Assert
            result.ReasonForFailure.Should().StartWith("No usable default constructor was found on the type");
        }
        public void Should_specify_that_sealed_types_cannot_be_generated()
        {
            // Arrange

            // Act
            var result = CastleDynamicProxyGenerator.GenerateProxy(typeof(SealedType), A.Dummy <IEnumerable <Type> >(), A.Dummy <IEnumerable <object> >(), Enumerable.Empty <Expression <Func <Attribute> > >(), A.Dummy <IFakeCallProcessorProvider>());

            // Assert
            result.ReasonForFailure.Should().Be("The type of proxy FakeItEasy.Tests.Creation.CastleDynamicProxy.CastleDynamicProxyGeneratorTests+SealedType is sealed.");
        }
        public void Should_specify_that_value_types_cannot_be_generated()
        {
            // Arrange

            // Act
            var result = CastleDynamicProxyGenerator.GenerateProxy(typeof(int), Enumerable.Empty <Type>(), null, Enumerable.Empty <Expression <Func <Attribute> > >(), A.Dummy <IFakeCallProcessorProvider>());

            // Assert
            result.ReasonForFailure.Should().Be("The type of proxy must be an interface or a class but it was System.Int32.");
        }
        public void Should_return_result_with_ProxyWasSuccessfullyGenerated_set_to_false_when_proxy_cannot_be_generated(Type typeOfProxy)
        {
            // Arrange

            // Act
            var result = CastleDynamicProxyGenerator.GenerateProxy(typeOfProxy, Enumerable.Empty <Type>(), null, Enumerable.Empty <Expression <Func <Attribute> > >(), A.Dummy <IFakeCallProcessorProvider>());

            // Assert
            result.ProxyWasSuccessfullyGenerated.Should().BeFalse();
        }
        public void Should_implement_additional_interfaces(Type typeOfProxy)
        {
            // Arrange

            // Act
            var result = CastleDynamicProxyGenerator.GenerateProxy(typeOfProxy, new[] { typeof(IFoo) }, null, Enumerable.Empty <Expression <Func <Attribute> > >(), A.Dummy <IFakeCallProcessorProvider>());

            // Assert
            result.GeneratedProxy.Should().NotBeNull().And.BeAssignableTo <IFoo>();
        }
        public void GenerateProxy_should_be_null_guarded()
        {
            // Arrange

            // Act

            // Assert
            Expression <Action> call = () => CastleDynamicProxyGenerator.GenerateProxy(typeof(IInterfaceType), Enumerable.Empty <Type>(), null, Enumerable.Empty <Expression <Func <Attribute> > >(), A.Dummy <IFakeCallProcessorProvider>());

            call.Should().BeNullGuarded();
        }
        public void Should_return_proxy_that_is_of_the_specified_type(Type typeOfProxy)
        {
            // Arrange

            // Act
            var result = CastleDynamicProxyGenerator.GenerateProxy(typeOfProxy, Enumerable.Empty <Type>(), null, Enumerable.Empty <Expression <Func <Attribute> > >(), A.Dummy <IFakeCallProcessorProvider>());

            // Assert
            result.GeneratedProxy.Should().NotBeNull()
            .And.Subject.Should().Match(p => typeOfProxy.IsInstanceOfType(p));
        }
        public void Should_specify_that_private_class_was_not_found()
        {
            // Arrange

            // Act
            var type   = Type.GetType("FluentAssertions.Common.NullReflector, FluentAssertions.Core");
            var result = CastleDynamicProxyGenerator.GenerateProxy(type, Enumerable.Empty <Type>(), null, Enumerable.Empty <Expression <Func <Attribute> > >(), A.Dummy <IFakeCallProcessorProvider>());

            // Assert
            result.ReasonForFailure.Should().StartWith("No usable default constructor was found on the type FluentAssertions.Common.NullReflector.\r\nAn exception of type Castle.DynamicProxy.Generators.GeneratorException was caught during this call. Its message was:\r\nCan not create proxy for type FluentAssertions.Common.NullReflector because it is not accessible. Make it public, or internal and mark your assembly with [assembly: InternalsVisibleTo(\"DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7\")] attribute, because assembly FluentAssertions.Core is strong-named.");
        }
        public void Should_fail_when_arguments_for_constructor_is_passed_with_interface_proxy()
        {
            // Arrange
            var arguments = new object[] { "no constructor on interface " };

            // Act
            var ex = Record.Exception(() => CastleDynamicProxyGenerator.GenerateProxy(typeof(IInterfaceType), Enumerable.Empty <Type>(), arguments, Enumerable.Empty <Expression <Func <Attribute> > >(), A.Dummy <IFakeCallProcessorProvider>()));

            // Assert
            ex.Should().BeAnExceptionOfType <ArgumentException>()
            .WithMessage("Arguments for constructor specified for interface type.");
        }
        public void Should_ensure_fake_call_processor_is_initialized_but_not_fetched_when_no_method_on_fake_is_called(Type typeThatImplementsInterfaceType)
        {
            // Arrange
            var fakeCallProcessorProvider = A.Fake <IFakeCallProcessorProvider>();

            // Act
            CastleDynamicProxyGenerator.GenerateProxy(typeThatImplementsInterfaceType, Enumerable.Empty <Type>(), null, Enumerable.Empty <Expression <Func <Attribute> > >(), fakeCallProcessorProvider);

            // Assert
            A.CallTo(() => fakeCallProcessorProvider.Fetch(A <object> ._)).MustNotHaveHappened();
            A.CallTo(() => fakeCallProcessorProvider.EnsureInitialized(A <object> ._)).MustHaveHappened();
        }
        public void Should_be_able_to_intercept_ToString(Type typeOfProxy)
        {
            // Arrange
            var fakeCallProcessorProvider = CreateFakeCallProcessorProvider(c => c.SetReturnValue("interception return value"));

            // Act
            var proxy          = CastleDynamicProxyGenerator.GenerateProxy(typeOfProxy, Enumerable.Empty <Type>(), null, Enumerable.Empty <Expression <Func <Attribute> > >(), fakeCallProcessorProvider);
            var toStringResult = proxy.GeneratedProxy.ToString();

            // Assert
            toStringResult.Should().Be("interception return value");
        }
        public void Serialized_proxies_should_deserialize_to_an_object(Type typeOfProxy)
        {
            // Arrange
            // Here we can't use A.Dummy<IFakeCallProcessorProvider>() because the EnsureInitialized() call within GenerateProxy()
            // triggers the Castle issue #65 (https://github.com/castleproject/Core/issues/65)
            var result = CastleDynamicProxyGenerator.GenerateProxy(typeOfProxy, Type.EmptyTypes, null, Enumerable.Empty <Expression <Func <Attribute> > >(), new SerializableFakeCallProcessorProvider());
            var proxy  = result.GeneratedProxy;

            // Act
            var deserializedProxy = BinarySerializationHelper.SerializeAndDeserialize(proxy);

            // Assert
            deserializedProxy.Should().NotBeNull();
        }
        public void Should_fail_with_correct_message_when_no_constructor_matches_the_passed_in_arguments()
        {
            // Arrange

            // Act
            var result = CastleDynamicProxyGenerator.GenerateProxy(
                typeof(TypeWithArgumentsForConstructor),
                Enumerable.Empty <Type>(),
                new object[] { "no constructor takes a string" },
                Enumerable.Empty <Expression <Func <Attribute> > >(),
                A.Dummy <IFakeCallProcessorProvider>());

            // Assert
            result.ReasonForFailure.Should().StartWith("No constructor matches the passed arguments for constructor.\r\nAn exception of type Castle.DynamicProxy.InvalidProxyConstructorArgumentsException was caught during this call. Its message was:\r\nCan not instantiate proxy of class: FakeItEasy.Tests.Creation.CastleDynamicProxy.CastleDynamicProxyGeneratorTests+TypeWithArgumentsForConstructor.\r\nCould not find a constructor that would match given arguments:\r\nSystem.String\r\n");
        }
Example #16
0
        public void GenerateClassProxy_should_be_null_guarded()
        {
            // Arrange

            // Act

            // Assert
            Expression <Action> call = () => CastleDynamicProxyGenerator.GenerateClassProxy(
                typeof(AbstractClass),
                this.noAdditionalInterfaces,
                Enumerable.Empty <object>(),
                Enumerable.Empty <Expression <Func <Attribute> > >(),
                A.Dummy <IFakeCallProcessorProvider>());

            call.Should().BeNullGuarded();
        }
        public void Should_pass_arguments_for_constructor_to_constructed_instance()
        {
            // Arrange

            // Act
            var result = CastleDynamicProxyGenerator.GenerateProxy(
                typeof(TypeWithArgumentsForConstructor),
                Enumerable.Empty <Type>(),
                new object[] { 10 },
                Enumerable.Empty <Expression <Func <Attribute> > >(),
                A.Dummy <IFakeCallProcessorProvider>());

            var proxy = (TypeWithArgumentsForConstructor)result.GeneratedProxy;

            // Assert
            proxy.Argument.Should().Be(10);
        }
Example #18
0
            public CreationResult CreateFakeInterface(Type typeOfFake, IProxyOptions proxyOptions)
            {
                if (proxyOptions.ArgumentsForConstructor is not null)
                {
                    throw new ArgumentException(DynamicProxyMessages.ArgumentsForConstructorOnInterfaceType);
                }

                var fakeCallProcessorProvider = this.fakeCallProcessorProviderFactory(typeOfFake, proxyOptions);
                var proxyGeneratorResult      = CastleDynamicProxyGenerator.GenerateInterfaceProxy(
                    typeOfFake,
                    proxyOptions.AdditionalInterfacesToImplement,
                    proxyOptions.Attributes,
                    fakeCallProcessorProvider);

                return(proxyGeneratorResult.ProxyWasSuccessfullyGenerated
                    ? CreationResult.SuccessfullyCreated(proxyGeneratorResult.GeneratedProxy)
                    : CreationResult.FailedToCreateFake(typeOfFake, proxyGeneratorResult.ReasonForFailure !));
            }
        public void Should_delegate_to_fake_call_processor_when_method_on_fake_is_called(Type typeThatImplementsInterfaceType)
        {
            // Arrange
            IInterceptedFakeObjectCall interceptedFakeObjectCall = null;

            var fakeCallProcessorProvider = CreateFakeCallProcessorProvider(c => interceptedFakeObjectCall = c);

            var result = CastleDynamicProxyGenerator.GenerateProxy(typeThatImplementsInterfaceType, Enumerable.Empty <Type>(), null, Enumerable.Empty <Expression <Func <Attribute> > >(), fakeCallProcessorProvider);

            var proxy = (IInterfaceType)result.GeneratedProxy;

            // Act
            proxy.Foo(1, 2);

            // Assert
            interceptedFakeObjectCall.Should().NotBeNull();
            interceptedFakeObjectCall.Arguments.Should().BeEquivalentTo(1, 2);
            interceptedFakeObjectCall.Method.Name.Should().Be(
                typeof(IInterfaceType).GetMethod(nameof(IInterfaceType.Foo)).Name);
            interceptedFakeObjectCall.FakedObject.Should().BeSameAs(proxy);
        }
Example #20
0
        public CastleDynamicProxyGeneratorTests()
        {
            this.interceptionValidator = A.Fake <CastleDynamicProxyInterceptionValidator>();

            this.generator = new CastleDynamicProxyGenerator(this.interceptionValidator);
        }