Пример #1
0
 // Mock MVC ViewEngines
 void AddViewEngine()
 {
     view       = Pleasure.Mock <IView>();
     viewEngine = Pleasure.Mock <IViewEngine>();
     viewEngine.Setup(r => r.FindView(Pleasure.MockIt.IsAny <ControllerContext>(), Pleasure.MockIt.IsAny <string>(), Pleasure.MockIt.IsAny <bool>())).Returns(ViewEngineResult.Found(view.Object.Path, view.Object));
     //ViewEngines.Engines.Clear();
     //ViewEngines.Engines.Add(viewEngine.Object);
 }
        public MockUrl StubRequest(Action <Mock <HttpRequest> > action)
        {
            var request = Pleasure.Mock <HttpRequest>();

            action(request);

            this.httpContext.SetupGet(r => r.Request).Returns(request.Object);
            return(this);
        }
        public MockUrl()
        {
            var routeData = new RouteData();

            routeData.Values.Add("Action", "Action");
            routeData.Values.Add("Controller", "Controller");
            this.httpContext = Pleasure.Mock <HttpContext>(mock =>
            {
                var httpRequestBase = Pleasure.MockAsObject <HttpRequest>(mock1 => mock1.SetupGet(r => r.PathBase).Returns("/"));
                mock.SetupGet(r => r.Request).Returns(httpRequestBase);
            });
            var actionContext = new ActionContext(this.httpContext.Object, routeData, new ActionDescriptor());

            Original = new UrlHelper(actionContext);
        }
Пример #4
0
        /// <summary>
        ///     Static method to construct MockController object
        /// </summary>
        /// <param name="ctorArgs">original controller constructor parameters</param>
        /// <returns></returns>
        public static MockController <TController> When(params object[] ctorArgs)
        {
            var dispatcher = Pleasure.Mock <IDispatcher>();

            // Return Mock when call to IoC helper IoCFactory.Instance.TryResolve()
            //IoCFactory.Instance.StubTryResolve(dispatcher.Object);

            var controller = (TController)Activator.CreateInstance(typeof(TController), ctorArgs.ToArray());
            var res        = new MockController <TController>(controller, dispatcher);

            res.httpContext.SetupGet(r => r.Request.Headers).Returns(new HeaderDictionary(new Dictionary <string, StringValues> {
                { "X-Requested-With", "XMLHttpRequest" }
            }));

            return(res);
        }
        static void StubInit(CachingInit cachingInit, Action <Mock <ICachedProvider> > action)
        {
            var mockProvider = Pleasure.Mock(action);

            if (cachingInit.Provider != null)
            {
                try
                {
                    mockProvider = Mock.Get(cachingInit.Provider);
                    action(mockProvider);
                }
                ////ncrunch: no coverage start
                catch (Exception)
                {
                    mockProvider = Pleasure.Mock(action);
                }

                ////ncrunch: no coverage end
            }

            cachingInit.WithProvider(mockProvider.Object);
        }
Пример #6
0
        /// <summary>
        /// </summary>
        /// <param name="controller">original controller</param>
        /// <param name="dispatcher">mock of IDispatcher</param>
        MockController(TController controller, Mock <IDispatcher> dispatcher)
        {
            this.dispatcher    = dispatcher;
            originalController = controller;

            httpContext = Pleasure.Mock <HttpContext>();

            // preparing RouteData for ControllerContext
            var routeData = new RouteData();

            routeData.Values.Add("Action", "Action");
            routeData.Values.Add("Controller", "Controller");

            var actionContext = new ActionContext(httpContext.Object, routeData, new ActionDescriptor());

            // replacing original controller Context with Mock objects
            originalController.ControllerContext = new ControllerContext(actionContext);

            // replacing original controller UrlHelper with Mock objects
            originalController.Url = new UrlHelper(actionContext);

            // replace MVC ViewEngines with Mock objects
            AddViewEngine();
        }
Пример #7
0
 public static void StubQuery <TEntity>(this Mock <IRepository> repository, OrderSpecification <TEntity> orderSpecification = null, Specification <TEntity> whereSpecification = null, FetchSpecification <TEntity> fetchSpecification = null, PaginatedSpecification paginatedSpecification = null, bool skipInterceptions = false, params TEntity[] entities) where TEntity : class, IEntity, new()
 {
     repository
     .Setup(r => r.Query(Pleasure.MockIt.IsStrong(whereSpecification, dsl => dsl.IncludeAllFields()), orderSpecification, fetchSpecification, Pleasure.MockIt.IsStrong(paginatedSpecification, dsl => dsl.IncludeAllFields()), skipInterceptions))
     .Returns(Pleasure.ToQueryable(entities));
 }
Пример #8
0
        object GenerateValueOrEmpty(Type propertyType, bool isEmpty)
        {
            object value      = null;
            bool   isNullable = propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>);

            propertyType = isNullable ? propertyType.GetGenericArguments()[0] : propertyType;

            if (propertyType.IsEnum)
            {
                value = isEmpty ? 0 : Enum.Parse(propertyType, Pleasure.Generator.EnumAsInt(propertyType).ToString(), true);
            }
            else if (propertyType.IsAnyEquals(typeof(string), typeof(object)))
            {
                value = isEmpty ? string.Empty : Pleasure.Generator.String();
            }
            else if (propertyType == typeof(bool) || propertyType == typeof(bool?))
            {
                // ReSharper disable SimplifyConditionalTernaryExpression
                value = isEmpty ? false : Pleasure.Generator.Bool();
            }
            // ReSharper restore SimplifyConditionalTernaryExpression
            else if (propertyType.IsAnyEquals(typeof(int)))
            {
                value = isEmpty ? default(int) : Pleasure.Generator.PositiveNumber(1);
            }
            else if (propertyType.IsAnyEquals(typeof(long)))
            {
                value = isEmpty ? default(long) : (long)Pleasure.Generator.PositiveNumber(1);
            }
            else if (propertyType.IsAnyEquals(typeof(short)))
            {
                value = isEmpty ? default(short) : (short)Pleasure.Generator.PositiveNumber(1);
            }
            else if (propertyType.IsAnyEquals(typeof(float)))
            {
                value = isEmpty ? default(float) : Pleasure.Generator.PositiveFloating();
            }
            else if (propertyType.IsAnyEquals(typeof(decimal)))
            {
                value = isEmpty ? default(decimal) : Pleasure.Generator.PositiveDecimal();
            }
            else if (propertyType.IsAnyEquals(typeof(double)))
            {
                value = isEmpty ? default(double) : Pleasure.Generator.PositiveDouble();
            }
            else if (propertyType.IsAnyEquals(typeof(byte), typeof(sbyte)))
            {
                value = isEmpty ? default(byte) : (byte)Pleasure.Generator.PositiveNumber();
            }
            else if (propertyType == typeof(char))
            {
                value = isEmpty ? default(char) : Pleasure.Generator.String()[0];
            }
            else if (propertyType.IsAnyEquals(typeof(DateTime)))
            {
                value = isEmpty ? new DateTime() : Pleasure.Generator.DateTime();
            }
            else if (propertyType.IsAnyEquals(typeof(TimeSpan)))
            {
                value = isEmpty ? new TimeSpan() : Pleasure.Generator.TimeSpan();
            }
            else if (propertyType.IsAnyEquals(typeof(Stream), typeof(MemoryStream)))
            {
                value = isEmpty ? Pleasure.Generator.Stream(0) : Pleasure.Generator.Stream();
            }
            else if (propertyType == typeof(byte[]))
            {
                value = isEmpty ? Pleasure.ToArray <byte>() : Pleasure.Generator.Bytes();
            }
            else if (propertyType == typeof(Guid))
            {
                value = isEmpty ? Guid.Empty : Guid.NewGuid();
            }
            else if (propertyType == typeof(int[]))
            {
                value = isEmpty ? Pleasure.ToArray <int>() : Pleasure.ToArray(Pleasure.Generator.PositiveNumber(1));
            }
            else if (propertyType == typeof(string[]))
            {
                value = isEmpty ? Pleasure.ToArray <string>() : Pleasure.ToArray(Pleasure.Generator.String());
            }
            else if (propertyType.IsAnyEquals(typeof(FormFile), typeof(IFormFile)))
            {
                value = isEmpty ? null : Pleasure.Generator.FormFile();
            }
            else if (propertyType == typeof(Dictionary <string, string>))
            {
                value = isEmpty ? new Dictionary <string, string>() : Pleasure.ToDynamicDictionary <string>(new { key = Pleasure.Generator.String() });
            }
            else if (propertyType == typeof(Dictionary <string, object>))
            {
                value = isEmpty ? new Dictionary <string, object>() : Pleasure.ToDynamicDictionary <string>(new { key = Pleasure.Generator.String() }).ToDictionary(r => r.Key, r => (object)r.Value);
            }
            //else if (propertyType == typeof(SqlConnection))
            //    value = new SqlConnection(@"Data Source={0};Database={1};Integrated Security=true;".F(Pleasure.Generator.String(length: 5), Pleasure.Generator.String(length: 5)));

            return(isNullable ? Activator.CreateInstance(typeof(Nullable <>).MakeGenericType(propertyType), value) : value);
        }