Exemplo n.º 1
0
        private static FakeObject GetFakeObjectMethodIsCalledOn(LambdaExpression expression)
        {
            var methodCall = expression.Body as MethodCallExpression;

            if (methodCall != null)
            {
                return(FakeItEasy.Fake.GetFakeObject(ExpressionManager.GetValueProducedByExpression(methodCall.Object)));
            }
            else
            {
                var propertyCall = expression.Body as MemberExpression;
                return(FakeItEasy.Fake.GetFakeObject(ExpressionManager.GetValueProducedByExpression(propertyCall.Expression)));
            }
        }
Exemplo n.º 2
0
        internal static IArgumentValidator GetArgumentValidatorForArgument(MethodCallExpression expression)
        {
            var argumentsForConstructor = expression.Arguments.Skip(1).Select(x => ExpressionManager.GetValueProducedByExpression(x)).ToArray();
            var validatorType           = GetValidatorType(expression);

            AssertValidatorTypeImplementsArgumentValidatorInterface(validatorType);

            EnsureThatValidatorConstructorMatchesSignatureOfExtensionMethod(expression.Method, validatorType);

            var validator = (IArgumentValidator)Activator.CreateInstance(validatorType, argumentsForConstructor);


            return(validator);
        }
Exemplo n.º 3
0
        public static T Fake <T>(Expression <Func <T> > constructorCall) where T : class
        {
            Guard.IsNotNull(constructorCall, "constructorCall");

            if (constructorCall.Body.NodeType != ExpressionType.New)
            {
                throw new ArgumentException(ExceptionMessages.NonConstructorExpressionMessage);
            }

            var constructorArguments =
                (from argument in ((NewExpression)constructorCall.Body).Arguments
                 select ExpressionManager.GetValueProducedByExpression(argument)).ToArray();

            return((T) new FakeObject(typeof(T), constructorArguments).Object);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a fake object and passes the arguments for the specified constructor call
        /// to the constructor of the fake object.
        /// </summary>
        /// <param name="constructorCall">An expression describing the constructor to be called
        /// on the faked object.</param>
        /// <exception cref="ArgumentNullException">The constructor call was null.</exception>
        public Fake(Expression <Func <T> > constructorCall)
        {
            Guard.IsNotNull(constructorCall, "constructorCall");

            if (constructorCall.Body.NodeType != ExpressionType.New)
            {
                throw new ArgumentException(ExceptionMessages.NonConstructorExpressionMessage);
            }

            var constructorArguments =
                from argument in ((NewExpression)constructorCall.Body).Arguments
                select ExpressionManager.GetValueProducedByExpression(argument);

            this.FakedObject = CreateFake <T>(constructorArguments);
        }