Пример #1
0
 public ChildScope(FakeScope parentScope, IFakeObjectContainer container)
 {
     this.parentScope              = parentScope;
     this.rulesField               = new Dictionary <FakeObject, List <CallRuleMetadata> >();
     this.recordedCalls            = new Dictionary <FakeObject, List <ICompletedFakeObjectCall> >();
     this.fakeObjectContainerField = container;
 }
 public void SetUp()
 {
     this.container         = A.Fake <IFakeObjectContainer>();
     this.proxyGenerator    = A.Fake <IProxyGenerator>();
     this.fakeObject        = A.Fake <FakeObject>();
     this.fakeObjectFactory = () => this.fakeObject;
 }
Пример #3
0
        /// <summary>
        /// Creates a new scope and sets it as the current scope, using the specified
        /// container as the container for the new scope.
        /// </summary>
        /// <param name="container">The container to use for the new scope.</param>
        /// <returns>The created scope.</returns>
        public static IFakeScope Create(IFakeObjectContainer container)
        {
            var result = new ChildScope(Current, container);

            Current = result;
            return(result);
        }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DummyValueCreationSession"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="fakeObjectCreator">The fake object creator.</param>
        public DummyValueCreationSession(IFakeObjectContainer container, IFakeObjectCreator fakeObjectCreator)
        {
            this.typesCurrentlyBeingResolved = new HashSet <Type>();
            this.strategyCache = new Dictionary <Type, ResolveStrategy>();
            this.strategies    = new ResolveStrategy[]
            {
                new ResolveFromContainerStrategy {
                    Container = container
                },
#if NET40
                new ResolveByCreatingTaskStrategy {
                    Session = this
                },
                new ResolveByCreatingLazyStrategy {
                    Session = this
                },
#endif
                new ResolveByCreatingFakeStrategy {
                    FakeCreator = fakeObjectCreator, Session = this
                },
                new ResolveByActivatingValueTypeStrategy(),
                new ResolveByInstantiatingClassUsingDummyValuesAsConstructorArgumentsStrategy {
                    Session = this
                }
            };
        }
Пример #5
0
        public void SetUp()
        {
            this.container         = A.Fake <IFakeObjectContainer>();
            this.fakeObjectCreator = A.Fake <IFakeObjectCreator>();

            this.session = new DummyValueCreationSession(this.container, this.fakeObjectCreator);
        }
Пример #6
0
        /// <summary>
        /// Gets a value indicating if a proxy of the specified type can be generated and sets the generated proxy
        /// to the out parameter if it can.
        /// </summary>
        /// <param name="typeToProxy">The type to generate a proxy for.</param>
        /// <param name="fakeObject">The generated proxy must implement the IFakedProxy interface and this is the fake object
        /// that should be returned for the call to GetFakeObject().</param>
        /// <param name="container">A fake object container the proxy generator can use to get arguments for constructor.</param>
        /// <returns>True if the proxy could be generated.</returns>
        /// <exception cref="ArgumentException">The arguments in argumentsForConstructor does not match any constructor
        /// of the proxied type.</exception>
        public ProxyResult GenerateProxy(Type typeToProxy, FakeObject fakeObject, IFakeObjectContainer container)
        {
            ProxyResult result = null;

            if (!this.TryGenerateProxy(typeToProxy, fakeObject, container, out result))
            {
                result = new DynamicProxyResult(typeToProxy, string.Empty);
            }
            
            return result;
        }
Пример #7
0
            private bool TryCreateProxiedArgument(Type type, IFakeObjectContainer container, out object argument)
            {
                ProxyResult result;
                if (this.TryGenerateProxy(type, null, out result))
                {
                    argument = result.Proxy;
                    return true;
                }

                argument = null;
                return false;
            }
Пример #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DummyValueCreationSession"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="fakeObjectCreator">The fake object creator.</param>
        public DummyValueCreationSession(IFakeObjectContainer container, IFakeObjectCreator fakeObjectCreator)
        {
            this.isInProcessOfResolving = new HashSet<Type>();
            this.strategyToUseForType = new Dictionary<Type, ResolveStrategy>();

            this.availableStrategies = new ResolveStrategy[]
                                           {
                                               new ResolveFromContainerSrategy { Container = container },
                                               new ResolveByCreatingFakeStrategy { FakeCreator = fakeObjectCreator, Session = this },
                                               new ResolveByActivatingValueTypeStrategy(),
                                               new ResolveByInstantiatingClassUsingDummyValuesAsConstructorArgumentsStrategy { Session = this }
                                           };
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DummyValueCreationSession"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="fakeObjectCreator">The fake object creator.</param>
        public DummyValueCreationSession(IFakeObjectContainer container, IFakeObjectCreator fakeObjectCreator)
        {
            this.typesCurrentlyBeingResolved = new HashSet<Type>();
            this.strategyCache = new Dictionary<Type, ResolveStrategy>();
            this.strategies = new ResolveStrategy[]
                {
                    new ResolveFromContainerStrategy { Container = container }, 
#if NET40
                    new ResolveByCreatingTaskStrategy { Session = this }, 
#endif
                    new ResolveByCreatingFakeStrategy { FakeCreator = fakeObjectCreator, Session = this }, 
                    new ResolveByActivatingValueTypeStrategy(), 
                    new ResolveByInstantiatingClassUsingDummyValuesAsConstructorArgumentsStrategy { Session = this }
                };
        }
Пример #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DummyValueCreationSession"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="fakeObjectCreator">The fake object creator.</param>
        public DummyValueCreationSession(IFakeObjectContainer container, IFakeObjectCreator fakeObjectCreator)
        {
            this.isInProcessOfResolving = new HashSet <Type>();
            this.strategyToUseForType   = new Dictionary <Type, ResolveStrategy>();

            this.availableStrategies = new ResolveStrategy[]
            {
                new ResolveFromContainerSrategy {
                    Container = container
                },
                new ResolveByCreatingFakeStrategy {
                    FakeCreator = fakeObjectCreator, Session = this
                },
                new ResolveByActivatingValueTypeStrategy(),
                new ResolveByInstantiatingClassUsingDummyValuesAsConstructorArgumentsStrategy {
                    Session = this
                }
            };
        }
Пример #11
0
        private IEnumerable <object> ResolveConstructorArguments(Type typeToProxy, IFakeObjectContainer container)
        {
            if (typeToProxy.IsInterface || TypeHasDefaultConstructor(typeToProxy))
            {
                return(null);
            }

            foreach (var constructor in GetUsableConstructors(typeToProxy))
            {
                var resolvedArguments = new List <object>();

                if (ResolveArgumentsFromTypes(constructor.GetParameters().Select(x => x.ParameterType), resolvedArguments, container))
                {
                    return(resolvedArguments);
                }
            }

            return(null);
        }
Пример #12
0
        /// <summary>
        /// Gets a value indicating if a proxy of the specified type can be generated and sets the generated proxy
        /// to the out parameter if it can.
        /// </summary>
        /// <param name="typeToProxy">The type to generate a proxy for.</param>
        /// <param name="fakeObject">The generated proxy must implement the IFakedProxy interface and this is the fake object
        /// that should be returned for the call to GetFakeObject().</param>
        /// <param name="container">A fake object container the proxy generator can use to get arguments for constructor.</param>
        /// <param name="generatedProxy">An object containing the proxy if generation was successful.</param>
        /// <returns>True if the proxy could be generated.</returns>
        /// <exception cref="ArgumentException">The arguments in argumentsForConstructor does not match any constructor
        /// of the proxied type.</exception>
        public bool TryGenerateProxy(Type typeToProxy, FakeObject fakeObject, IFakeObjectContainer container, out ProxyResult result)
        {
            if (typeToProxy.IsInterface)
            {
                result = GenerateInterfaceProxy(typeToProxy, fakeObject);
                return(true);
            }

            var argumentsForConstructor = this.ResolveConstructorArguments(typeToProxy, container);

            if (!TypeCanBeProxiedWithArgumentsForConstructor(typeToProxy, argumentsForConstructor))
            {
                result = null;
                return(false);
            }

            result = GenerateClassProxy(typeToProxy, argumentsForConstructor, fakeObject);
            return(true);
        }
Пример #13
0
        public static void CallFromConstructor(
            IFakeObjectContainer fakeObjectContainer,
            MakesVirtualCallInConstructor fake,
            string virtualMethodValueInsideOfScope,
            string virtualMethodValueOutsideOfScope)
        {
            "establish"
            .x(() =>
            {
                fakeObjectContainer = A.Fake <IFakeObjectContainer>();
                A.CallTo(() => fakeObjectContainer.ConfigureFake(A <Type> ._, A <object> ._))
                .Invokes(
                    (Type t, object options) =>
                    A.CallTo(options).WithReturnType <string>().Returns("configured value in fake scope"));
            });

            "when configuring a method called by a constructor from within a scope"
            .x(() =>
            {
                using (Fake.CreateScope(fakeObjectContainer))
                {
                    fake = A.Fake <MakesVirtualCallInConstructor>();
                    virtualMethodValueInsideOfScope = fake.VirtualMethod(null);
                }

                virtualMethodValueOutsideOfScope = fake.VirtualMethod(null);
            });

            "it should use the fake object container to configure the fake"
            .x(() => A.CallTo(() => fakeObjectContainer.ConfigureFake(typeof(MakesVirtualCallInConstructor), fake))
               .MustHaveHappened());

            "it should return the configured value within the scope during the constructor"
            .x(() => fake.VirtualMethodValueDuringConstructorCall.Should().Be("configured value in fake scope"));

            "it should return the configured value within the scope after the constructor"
            .x(() => virtualMethodValueInsideOfScope.Should().Be("configured value in fake scope"));

            "it should return default value outside the scope"
            .x(() => virtualMethodValueOutsideOfScope.Should().Be(string.Empty));
        }
Пример #14
0
        public static void CallFromConstructor(
            IFakeObjectContainer fakeObjectContainer,
            MakesVirtualCallInConstructor fake,
            string virtualMethodValueInsideOfScope,
            string virtualMethodValueOutsideOfScope)
        {
            "establish"
                .x(() =>
                    {
                        fakeObjectContainer = A.Fake<IFakeObjectContainer>();
                        A.CallTo(() => fakeObjectContainer.ConfigureFake(A<Type>._, A<object>._))
                            .Invokes(
                                (Type t, object options) =>
                                A.CallTo(options).WithReturnType<string>().Returns("configured value in fake scope"));
                    });

            "when configuring a method called by a constructor from within a scope"
                .x(() =>
                    {
                        using (Fake.CreateScope(fakeObjectContainer))
                        {
                            fake = A.Fake<MakesVirtualCallInConstructor>();
                            virtualMethodValueInsideOfScope = fake.VirtualMethod(null);
                        }

                        virtualMethodValueOutsideOfScope = fake.VirtualMethod(null);
                    });

            "it should use the fake object container to configure the fake"
                .x(() => A.CallTo(() => fakeObjectContainer.ConfigureFake(typeof(MakesVirtualCallInConstructor), fake))
                             .MustHaveHappened());

            "it should return the configured value within the scope during the constructor"
                .x(() => fake.VirtualMethodValueDuringConstructorCall.Should().Be("configured value in fake scope"));

            "it should return the configured value within the scope after the constructor"
                .x(() => virtualMethodValueInsideOfScope.Should().Be("configured value in fake scope"));

            "it should return default value outside the scope"
                .x(() => virtualMethodValueOutsideOfScope.Should().Be(string.Empty));
        }
Пример #15
0
        public static void UsingFakeOutsideOfScope(
            IFakeObjectContainer fakeObjectContainer,
            MakesVirtualCallInConstructor fake,
            string virtualMethodValueOutsideOfScope)
        {
            "given an object container"
                .x(() => fakeObjectContainer = CreateFakeObjectContainer("configured value in fake scope"));

            "and a fake created within a scope using that container"
                .x(() =>
                {
                    using (Fake.CreateScope(fakeObjectContainer))
                    {
                        fake = A.Fake<MakesVirtualCallInConstructor>();
                    }
                });

            "when the fake is accessed outside the scope"
                .x(() => virtualMethodValueOutsideOfScope = fake.VirtualMethod("call outside scope"));

            "then the object container's configuration should not be used"
                .x(() => virtualMethodValueOutsideOfScope.Should().Be(string.Empty));
        }
Пример #16
0
        public static void UsingFakeOutsideOfScope(
            IFakeObjectContainer fakeObjectContainer,
            MakesVirtualCallInConstructor fake,
            string virtualMethodValueOutsideOfScope)
        {
            "given an object container"
            .x(() => fakeObjectContainer = CreateFakeObjectContainer("configured value in fake scope"));

            "and a fake created within a scope using that container"
            .x(() =>
            {
                using (Fake.CreateScope(fakeObjectContainer))
                {
                    fake = A.Fake <MakesVirtualCallInConstructor>();
                }
            });

            "when the fake is accessed outside the scope"
            .x(() => virtualMethodValueOutsideOfScope = fake.VirtualMethod("call outside scope"));

            "then the object container's configuration should not be used"
            .x(() => virtualMethodValueOutsideOfScope.Should().Be(string.Empty));
        }
Пример #17
0
        public static void CreatingFakeInsideScope(
            IFakeObjectContainer fakeObjectContainer,
            IFakeScope scope,
            MakesVirtualCallInConstructor fake)
        {
            "given an object container"
            .x(() => fakeObjectContainer = CreateFakeObjectContainer("configured value in fake scope"));

            "and a fake scope using that container"
            .x(context => scope = Fake.CreateScope(fakeObjectContainer).Using(context));

            "when a fake is created inside the scope"
            .x(() => fake = A.Fake <MakesVirtualCallInConstructor>());

            "then the object container should configure the fake"
            .x(() => A.CallTo(() => fakeObjectContainer.BuildOptions(typeof(MakesVirtualCallInConstructor), A <IFakeOptions> ._))
               .MustHaveHappened());

            "and the object container's configuration should be used during the constructor"
            .x(() => fake.VirtualMethodValueDuringConstructorCall.Should().Be("configured value in fake scope"));

            "and the object container's configuration should be used after the constructor"
            .x(() => fake.VirtualMethod("call after constructor").Should().Be("configured value in fake scope"));
        }
Пример #18
0
        public static void CreatingFakeInsideScope(
            IFakeObjectContainer fakeObjectContainer,
            IFakeScope scope,
            MakesVirtualCallInConstructor fake)
        {
            "given an object container"
                .x(() => fakeObjectContainer = CreateFakeObjectContainer("configured value in fake scope"));

            "and a fake scope using that container"
                .x(context => scope = Fake.CreateScope(fakeObjectContainer).Using(context));

            "when a fake is created inside the scope"
                .x(() => fake = A.Fake<MakesVirtualCallInConstructor>());

            "then the object container should configure the fake"
                .x(() => A.CallTo(() => fakeObjectContainer.ConfigureFake(typeof(MakesVirtualCallInConstructor), fake))
                    .MustHaveHappened());

            "and the object container's configuration should be used during the constructor"
                .x(() => fake.VirtualMethodValueDuringConstructorCall.Should().Be("configured value in fake scope"));

            "and the object container's configuration should be used after the constructor"
                .x(() => fake.VirtualMethod("call after constructor").Should().Be("configured value in fake scope"));
        }
Пример #19
0
 public RootScope()
 {
     this.fakeObjectContainerField = new DynamicContainer(
         ServiceLocator.Current.Resolve <IEnumerable <IDummyDefinition> >(),
         ServiceLocator.Current.Resolve <IEnumerable <IFakeConfigurator> >());
 }
Пример #20
0
 /// <summary>
 /// Creates a new scope and sets it as the current scope. When inside a scope the
 /// getting the calls made to a fake will return only the calls within that scope and when
 /// asserting that calls were made, the calls must have been made within that scope.
 /// </summary>
 /// <param name="container">The container to use within the specified scope.</param>
 /// <returns>The created scope.</returns>
 public static IFakeScope CreateScope(IFakeObjectContainer container)
 {
     return(Facade.CreateScope(container));
 }
Пример #21
0
        public virtual IFakeScope CreateScope(IFakeObjectContainer container)
        {
            Guard.AgainstNull(container, "container");

            return this.fakeScopeFactory.Create(container);
        }
Пример #22
0
        private bool ResolveArgumentsFromTypes(IEnumerable <Type> argumentTypes, ICollection <object> arguments, IFakeObjectContainer container)
        {
            foreach (var argumentType in argumentTypes)
            {
                object resolvedArgument = null;
                if (container.TryCreateFakeObject(argumentType, null, out resolvedArgument))
                {
                    arguments.Add(resolvedArgument);
                }
                else if (TryCreateValueTypeArgument(argumentType, out resolvedArgument))
                {
                    arguments.Add(resolvedArgument);
                }
                else if (TryCreateProxiedArgument(argumentType, container, out resolvedArgument))
                {
                    arguments.Add(resolvedArgument);
                }
                else
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #23
0
 /// <summary>
 /// Creates a new scope and sets it as the current scope. When inside a scope the
 /// getting the calls made to a fake will return only the calls within that scope and when
 /// asserting that calls were made, the calls must have been made within that scope.
 /// </summary>
 /// <param name="container">The container to use within the specified scope.</param>
 /// <returns>The created scope.</returns>
 public static IFakeScope CreateScope(IFakeObjectContainer container)
 {
     return Facade.CreateScope(container);
 }
Пример #24
0
 public RootScope()
 {
     this.fakeObjectContainerField = new DynamicContainer(
         ServiceLocator.Current.Resolve <IEnumerable <IDummyFactory> >(),
         ServiceLocator.Current.Resolve <IEnumerable <IFakeOptionsBuilder> >());
 }
Пример #25
0
 /// <summary>
 /// Creates a new scope and sets it as the current scope. When inside a scope the
 /// getting the calls made to a fake will return only the calls within that scope and when
 /// asserting that calls were made, the calls must have been made within that scope.
 /// </summary>
 /// <param name="container">The container to use within the specified scope.</param>
 /// <returns>The created scope.</returns>
 public static IDisposable CreateScope(IFakeObjectContainer container)
 {
     return(FakeScope.Create(container));
 }
Пример #26
0
 public RootScope()
 {
     this.fakeObjectContainerField = this.ResolveContainer();
 }
Пример #27
0
 public RootScope()
 {
     this.fakeObjectContainerField = new DynamicContainer(
         ServiceLocator.Current.Resolve<IEnumerable<IDummyDefinition>>(),
         ServiceLocator.Current.Resolve<IEnumerable<IFakeConfigurator>>());
 }
 public ProxyResult GenerateProxy(Type typeToProxy, FakeObject fakeObject, IFakeObjectContainer container)
 {
     return(this.ProxyGeneratorFake.GenerateProxy(typeToProxy, fakeObject, container));
 }
Пример #29
0
 public IFakeScope Create(IFakeObjectContainer container)
 {
     return(FakeScope.Create(container));
 }
Пример #30
0
 public IFakeScope Create(IFakeObjectContainer container)
 {
     return FakeScope.Create(container);
 }
Пример #31
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FakeObjectFactory"/> class.
 /// </summary>
 /// <param name="container">The container to use.</param>
 /// <param name="proxyGenerator">The proxy generator to use.</param>
 /// <param name="fakeObjectFactory">The fake object factory to use.</param>
 public FakeObjectFactory(IFakeObjectContainer container, IProxyGenerator proxyGenerator, FakeObject.Factory fakeObjectFactory)
 {
     this.container         = container;
     this.proxyGenerator    = proxyGenerator;
     this.fakeObjectFactory = fakeObjectFactory;
 }
Пример #32
0
 public ChildScope(FakeScope parentScope, IFakeObjectContainer container)
 {
     this.parentScope = parentScope;
     this.rulesField = new Dictionary<FakeManager, List<CallRuleMetadata>>();
     this.recordedCallsGroupedByFakeManager = new Dictionary<FakeManager, List<ICompletedFakeObjectCall>>();
     this.recordedCalls = new LinkedList<ICompletedFakeObjectCall>();
     this.fakeObjectContainerField = container;
 }
Пример #33
0
 public RootScope()
 {
     this.fakeObjectContainerField = new DynamicContainer(
         ServiceLocator.Current.Resolve<IEnumerable<IDummyFactory>>(), 
         ServiceLocator.Current.Resolve<IEnumerable<IFakeOptionsBuilder>>());
 }
Пример #34
0
 /// <summary>
 /// Creates a new scope and sets it as the current scope, using the specified
 /// container as the container for the new scope.
 /// </summary>
 /// <param name="container">The container to usee for the new scope.</param>
 /// <returns>The created scope.</returns>
 public static IFakeScope Create(IFakeObjectContainer container)
 {
     var result = new ChildScope(Current, container);
     Current = result;
     return result;
 }
Пример #35
0
        public virtual IFakeScope CreateScope(IFakeObjectContainer container)
        {
            Guard.AgainstNull(container, "container");

            return(this.fakeScopeFactory.Create(container));
        }
Пример #36
0
 /// <summary>
 /// Gets a value indicating if a proxy of the specified type can be generated and sets the generated proxy
 /// to the out parameter if it can.
 /// </summary>
 /// <param name="typeToProxy">The type to generate a proxy for.</param>
 /// <param name="fakeObject">The generated proxy must implement the IFakedProxy interface and this is the fake object
 /// that should be returned for the call to GetFakeObject().</param>
 /// <param name="container">A fake object container the proxy generator can use to get arguments for constructor.</param>
 /// <param name="generatedProxy">An object containing the proxy if generation was successful.</param>
 /// <returns>True if the proxy could be generated.</returns>
 /// <exception cref="ArgumentException">The arguments in argumentsForConstructor does not match any constructor
 /// of the proxied type.</exception>
 private bool TryGenerateProxy(Type typeToProxy, FakeObject fakeObject, IFakeObjectContainer container, out ProxyResult result)
 {
     var request = new ConstructorResolvingProxyGenerationRequest(container);
     return request.TryGenerateProxy(typeToProxy, fakeObject, out result);
 }
Пример #37
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConstructorResolvingProxyGenerationRequest"/> class.
 /// </summary>
 /// <param name="container">The container.</param>
 public ConstructorResolvingProxyGenerationRequest(IFakeObjectContainer container)
 {
     this.container = container;
     this.constructorsCurrentlyBeingResolved = new HashSet<ConstructorInfo>();
 }
Пример #38
0
 public void SetUp()
 {
     this.fakeObject = A.Fake <FakeObject>();
     this.container  = A.Fake <IFakeObjectContainer>();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FakeObjectFactory"/> class.
 /// </summary>
 /// <param name="container">The container to use.</param>
 public FakeObjectFactory(IFakeObjectContainer container)
 {
     this.container = container;
 }