Exemplo n.º 1
0
        /// <summary>
        /// Gets a list of methods to configure.
        /// </summary>
        /// <param name="type">The type being mocked and whose methods need to be configured.</param>
        private static IEnumerable <MethodInfo> GetConfigurableMethods(Type type)
        {
            IEnumerable <MethodInfo> methods;

            if (DelegateSpecification.IsSatisfiedBy(type))
            {
                // If "type" is a delegate, return "Invoke" method only and skip the rest of the methods.
                methods = new[] { type.GetTypeInfo().GetMethod("Invoke") }
            }
            ;
            else if (type.GetTypeInfo().IsInterface)
            {
                // If "type" is an interface, "GetMethods" does not return methods declared on other interfaces extended by "type".
                // In these cases, we use the "GetInterfaceMethods" extension method instead.
                methods = type.GetInterfaceMethods();
            }
            else
            {
                methods = type.GetMethods();
            }

            // Skip properties that have both getters and setters to not interfere
            // with other post-processors in the chain that initialize them later.
            var getMethods = type.GetProperties()
                             .Where(p => p.GetGetMethod() != null &&
                                    p.GetSetMethod() != null)
                             .Select(p => p.GetGetMethod());

            methods = methods.Except(getMethods);

            return(methods.Where(CanBeConfigured));
        }
        /// <summary>
        /// Gets a list of methods to configure.
        /// </summary>
        /// <param name="type">The type being mocked and whose methods need to be configured.</param>
        private static IEnumerable <MethodInfo> GetConfigurableMethods(Type type)
        {
            // If "type" is a delegate, return "Invoke" method only and skip the rest of the methods.
            var methods = DelegateSpecification.IsSatisfiedBy(type)
                ? new[] { type.GetTypeInfo().GetMethod("Invoke") }
                : type.GetAllMethods();

            // Skip properties that have both getters and setters to not interfere
            // with other post-processors in the chain that initialize them later.
            methods = SkipWritablePropertyGetters(type, methods);

            return(methods.Where(CanBeConfigured));
        }
        public void IsSatisfiedByReturnsCorrectResult(object request, bool expectedResult)
        {
            // Arrange
            var sut = new DelegateSpecification();
            // Act
            var result = sut.IsSatisfiedBy(request);

            // Assert
            Assert.Equal(expectedResult, result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Selects constructors for the supplied <see cref="Moq.Mock{T}"/> type.
        /// </summary>
        /// <param name="type">The mock type.</param>
        /// <returns>
        /// Constructors for <paramref name="type"/>.
        /// </returns>
        /// <remarks>
        /// <para>
        /// This method only returns constructors if <paramref name="type"/> is a
        /// <see cref="Moq.Mock{T}"/> type. If not, an empty sequence is returned.
        /// </para>
        /// <para>
        /// If the type is the type of a constructed <see cref="Moq.Mock{T}"/>, constructors are
        /// returned according to the generic type argument's constructors. If the type is an
        /// interface, the <see cref="Moq.Mock{T}()"/> default constructor is returned. If the type
        /// is a class, constructors are returned according to all the public and protected
        /// constructors of the underlying type. In this case, the
        /// <see cref="Moq.Mock{T}(object[])"/> constructor that takes a params array is returned
        /// for each underlying constructor, with information about the appropriate parameters for
        /// each constructor.
        /// </para>
        /// </remarks>
        public IEnumerable <IMethod> SelectMethods(Type type)
        {
            if (!type.IsMock())
            {
                return(Enumerable.Empty <IMethod>());
            }

            var mockType = type.GetMockedType();

            if (mockType.GetTypeInfo().IsInterface || DelegateSpecification.IsSatisfiedBy(mockType))
            {
                return(new[] { new ConstructorMethod(type.GetDefaultConstructor()) });
            }

            return(from ci in mockType.GetPublicAndProtectedConstructors()
                   let paramInfos = ci.GetParameters()
                                    orderby paramInfos.Length ascending
                                    select new MockConstructorMethod(type.GetParamsConstructor(), paramInfos) as IMethod);
        }