コード例 #1
0
        /// <summary>
        /// Gets the default result for an invocation.
        /// </summary>
        /// <param name="invocation">The invocation.</param>
        /// <returns>The default value to return as result of the invocation.
        /// <see cref="Missing.Value"/> if no default value was provided.</returns>
        private object GetStubResult(Invocation invocation)
        {
            Type        returnType      = invocation.MethodReturnType;
            /* ^ */ var _returnTypeInfo = returnType.GetTypeInfo();             /* [email protected] ^ */

            // void method
            if (returnType == typeof(void))
            {
                return(Missing.Value);
            }

            // see if developer provides a return value
            object returnValue = MockFactory.ResolveType(invocation.Receiver, returnType);

            if (returnValue != Missing.Value)
            {
                return(returnValue);
            }

            if (/* ^ */ _returnTypeInfo.IsValueType /* [email protected] ^ */)
            {
                // use default contructor for value types
                return(Activator.CreateInstance(returnType));
            }

            if (returnType == typeof(string))
            {
                // string empty for strings
                return(string.Empty);
            }

            if (/* ^ */ _returnTypeInfo.IsClass && _returnTypeInfo.ImplementedInterfaces.Any(t => t == typeof(IEnumerable)) /* [email protected] ^ */)
            {
                // for enumerables (List, Dictionary) we create an empty object
                return(Activator.CreateInstance(returnType));
            }

            if (/* ^ */ _returnTypeInfo.IsSealed /* [email protected] ^ */)
            {
                // null for sealed classes
                return(null);
            }

            // a mock for interfaces and all cases no covered above
            return(MockFactory.NewMock(returnType, GetMemberName(invocation), MockFactory.GetDependencyMockStyle(invocation.Receiver, returnType) ?? MockStyle));
        }
コード例 #2
0
        /// <summary>
        /// Gets the default result for an invocation.
        /// </summary>
        /// <param name="invocation">The invocation.</param>
        /// <returns>The default value to return as result of the invocation.
        /// <see cref="Missing.Value"/> if no default value was provided.</returns>
        private object GetStubResult(Invocation invocation)
        {
            Type returnType = invocation.MethodReturnType;

            // void method
            if (returnType == typeof(void))
            {
                return(Missing.Value);
            }

            // see if developer provides a return value
            object returnValue = MockFactory.ResolveType(invocation.Receiver, returnType);

            if (returnValue != Missing.Value)
            {
                return(returnValue);
            }

            if (returnType.IsValueType)
            {
                // use default contructor for value types
                return(Activator.CreateInstance(returnType));
            }

            if (returnType == typeof(string))
            {
                // string empty for strings
                return(string.Empty);
            }

            if (returnType.IsClass && returnType.GetInterface("IEnumerable", false) != null)
            {
                // for enumerables (List, Dictionary) we create an empty object
                return(Activator.CreateInstance(returnType));
            }

            if (returnType.IsSealed)
            {
                // null for sealed classes
                return(null);
            }

            // a mock for interfaces and all cases no covered above
            return(MockFactory.NewMock(returnType, GetMemberName(invocation), MockFactory.GetDependencyMockStyle(invocation.Receiver, returnType) ?? MockStyle));
        }