예제 #1
0
        public void Intercept_InterceptsProperties_SetsExpectedPropertyValue()
        {
            //Arrange
            var service = Substitute.For <IAbstractService>();
            var config  = new StubAbstractTypeConfiguration();
            var context = Substitute.For <AbstractTypeCreationContext>();

            context.Options = new GetOptions {
                Lazy = LazyLoading.Enabled
            };

            var propertyName = "Property1";

            var info = typeof(IStubTarget).GetProperty(propertyName);

            var property = new StubAbstractPropertyConfiguration();

            property.PropertyInfo = info;
            var mapper = new StubAbstractDataMapper();

            var expected = "test random 1";

            var info1 = new FakePropertyInfo(typeof(string), propertyName, typeof(IStubTarget));

            config.AddProperty(property);
            config.Type = typeof(IStubTarget);

            property.Mapper       = mapper;
            property.PropertyInfo = info1;

            var args = new ObjectConstructionArgs(null, context, config, service)
            {
                Parameters = new Dictionary <string, object>()
            };
            var interceptor = new InterfacePropertyInterceptor(args, new LazyLoadingHelper());

            var setInvocation = Substitute.For <IInvocation>();

            setInvocation.Arguments.Returns(new[] { expected });
            setInvocation.Method.Returns(typeof(IStubTarget).GetProperty(propertyName).GetSetMethod());

            //Act
            interceptor.Intercept(setInvocation);

            //Assert
            var getInvocation = Substitute.For <IInvocation>();

            getInvocation.Method.Returns(typeof(IStubTarget).GetProperty(propertyName).GetGetMethod());

            interceptor.Intercept(getInvocation);

            Assert.AreEqual(expected, getInvocation.ReturnValue);
        }
        public void Intercept_WithMultiplePropertiesAndLazyLoading_ReturnsExpectedPropertyValues()
        {
            //Arrange
            var service = Substitute.For <IAbstractService>();
            var config  = new StubAbstractTypeConfiguration();
            var context = Substitute.For <AbstractTypeCreationContext>();

            config.Type    = typeof(IStubTarget);
            context.IsLazy = true;

            var args = new ObjectConstructionArgs(null, context, config, service)
            {
                Parameters = new Dictionary <string, object>()
            };
            var interceptor = new InterfacePropertyInterceptor(args);
            var invocations = new List <IInvocation>();

            for (var i = 0; i < 100; i++)
            {
                invocations.Add(CreateInterceptedProperty(config, i.ToString()));
            }

            //Act
            foreach (var invocation in invocations)
            {
                interceptor.Intercept(invocation);

                // Assert
                Assert.AreEqual(invocation.Method.Name.Substring(4), invocation.ReturnValue);
            }
        }
        public void Intercept_InterceptsProperties_ReturnsExpectedPropertyValue()
        {
            //Arrange
            var service = Substitute.For <IAbstractService>();
            var config  = new StubAbstractTypeConfiguration();
            var context = Substitute.For <AbstractTypeCreationContext>();

            var property = new StubAbstractPropertyConfiguration();
            var mapper   = new StubAbstractDataMapper();

            var expected     = "test random 1";
            var propertyName = "Property1";

            var info = new FakePropertyInfo(typeof(string), propertyName, typeof(IStubTarget));

            config.AddProperty(property);
            config.Type = typeof(IStubTarget);

            property.Mapper       = mapper;
            property.PropertyInfo = info;

            mapper.Value = expected;

            var args = new ObjectConstructionArgs(null, context, config, service)
            {
                Parameters = new Dictionary <string, object>()
            };
            var interceptor = new InterfacePropertyInterceptor(args);

            var invocation = Substitute.For <IInvocation>();

            invocation.Method.Returns(typeof(IStubTarget).GetProperty(propertyName).GetGetMethod());

            //Act
            interceptor.Intercept(invocation);

            //Assert
            Assert.AreEqual(expected, invocation.ReturnValue);
        }