Exemple #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);
        }
Exemple #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);
        }
Exemple #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();
        }