Beispiel #1
0
            public MockController(IControllerPackage <T> controllerPackage) : base(controllerPackage)
            {
                TextWriter = new StreamWriter(new MemoryStream());

                var routeData = new RouteData();

                routeData.Values.Add("controller", "PersonsController");
                var view             = new Mock <IView>();
                var engine           = new Mock <IViewEngine>();
                var viewEngineResult = new ViewEngineResult(view.Object, engine.Object);

                MockControllerContext.SetupGet(_ => _.HttpContext.User.Identity.Name)
                .Returns("admin");
                MockControllerContext.SetupGet(_ => _.HttpContext.Request.IsAuthenticated)
                .Returns(true);
                MockControllerContext.SetupGet(_ => _.HttpContext.Request.QueryString)
                .Returns(new NameValueCollection
                {
                    { "fkName", "UserId" },
                    { "fkValue", $"{FilteredUserId}" }
                });
                MockControllerContext.SetupGet(_ => _.HttpContext.User.Identity.IsAuthenticated)
                .Returns(true);
                MockControllerContext.SetupGet(m => m.RouteData)
                .Returns(routeData);
                MockControllerContext.SetupGet(_ => _.HttpContext.Response)
                .Returns(MockHttpResponse.Object);
                MockHttpResponse.SetupSet(_ => _.StatusCode = It.IsAny <int>())
                .Callback <int>(value => _statusCode        = value);
                engine.Setup(e => e.FindPartialView(It.IsAny <ControllerContext>(), It.IsAny <string>(), It.IsAny <bool>()))
                .Returns(viewEngineResult);

                ViewEngines.Engines.Clear();
                ViewEngines.Engines.Add(engine.Object);
                ViewData          = new ViewDataDictionary();
                ControllerContext = MockControllerContext.Object;
            }