Esempio n. 1
0
        /// <summary>
        /// Resolves requested types with Mock instances.
        /// </summary>
        /// <param name="context">The resolution context.</param>
        public void Resolve(MockResolutionContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (!(context.Value is null))
            {
                return;
            }

            var mockType = typeof(Mock <>).MakeGenericType(context.RequestType);

            bool mayHaveDependencies = context.RequestType.IsClass &&
                                       !typeof(Delegate).IsAssignableFrom(context.RequestType);

            object?[] constructorArgs = mayHaveDependencies
                ? context.AutoMocker.CreateArguments(context.RequestType, context.ObjectGraphContext)
                : Array.Empty <object>();

            if (Activator.CreateInstance(mockType, _mockBehavior, constructorArgs) is Mock mock)
            {
                mock.DefaultValue = _defaultValue;
                mock.CallBase     = _callBase;
                context.Value     = mock;
            }
        }
Esempio n. 2
0
        public void Resolve(MockResolutionContext context)
        {
            var(am, serviceType, value) = context ?? throw new ArgumentNullException(nameof(context));

            if (am.TryCompileGetter(serviceType, out var @delegate))
            {
                context.Value = @delegate;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Resolves IEnumerable&lt;T&gt; types.
        /// </summary>
        /// <param name="context">The resolution context.</param>
        public void Resolve(MockResolutionContext context)
        {
            var(am, serviceType, _) = context ?? throw new ArgumentNullException(nameof(context));

            if (!serviceType.GetTypeInfo().IsGenericType || serviceType.GetGenericTypeDefinition() != typeof(IEnumerable <>))
            {
                return;
            }

            var elementType = serviceType.GetGenericArguments().Single();
            var array       = System.Array.CreateInstance(elementType, 1);

            array.SetValue(am.Get(elementType), 0);
            context.Value = array;
        }
Esempio n. 4
0
        public void Resolve(MockResolutionContext context)
        {
            var(am, serviceType, value) = context ?? throw new ArgumentNullException(nameof(context));

            if (!serviceType.GetTypeInfo().IsGenericType || serviceType.GetGenericTypeDefinition() != typeof(Lazy <>))
            {
                return;
            }

            var returnType = serviceType.GetGenericArguments().Single();

            if (am.TryCompileGetter(typeof(Func <>).MakeGenericType(returnType), out var @delegate))
            {
                var lazyType = typeof(Lazy <>).MakeGenericType(returnType);
                context.Value = Activator.CreateInstance(lazyType, @delegate);
            }
        }
Esempio n. 5
0
        public void Resolve(MockResolutionContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (!(context.Value is null))
            {
                return;
            }

            var mockType = typeof(Mock <>).MakeGenericType(context.RequestType);

            if (Activator.CreateInstance(mockType, _mockBehavior) is Mock mock)
            {
                mock.DefaultValue = _defaultValue;
                mock.CallBase     = _callBase;
                context.Value     = mock;
            }
        }