コード例 #1
0
        public void ViewDataGetterShouldThrowNullReferenceExceptionWithNull()
        {
            MyApplication.StartsFrom <DefaultStartup>();

            var helper = ViewDataPropertyHelper.GetViewDataProperties <MvcController>();

            Assert.Throws <NullReferenceException>(() => helper.ViewDataGetter(null));
        }
コード例 #2
0
        public void ViewDataGetterShouldThrowInvalidOperationExceptionForPrivatePocoController()
        {
            MyApplication.StartsFrom <DefaultStartup>();

            var helper     = ViewDataPropertyHelper.GetViewDataProperties <PrivatePocoController>();
            var controller = new PrivatePocoController(TestServiceProvider.Global);

            Test.AssertException <InvalidOperationException>(
                () =>
            {
                helper.ViewDataGetter(controller);
            },
                "ViewDataDictionary could not be found on the provided PrivatePocoController. The property should be specified manually by providing component instance or using the specified helper methods.");
        }
コード例 #3
0
        public void GetPropertiesShouldNotThrowExceptionForNormalController()
        {
            MyApplication.StartsFrom <DefaultStartup>();

            var helper = ViewDataPropertyHelper.GetViewDataProperties <MvcController>();

            var controller        = new MvcController();
            var controllerContext = new ControllerContext
            {
                HttpContext = new DefaultHttpContext
                {
                    RequestServices = TestServiceProvider.Global
                }
            };

            var gotViewData = helper.ViewDataGetter(controller);

            Assert.NotNull(gotViewData);
            Assert.Same(gotViewData, controller.ViewData);
        }
コード例 #4
0
        public void GetPropertiesShouldNotThrowExceptionForPocoController()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services =>
            {
                services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            });

            var helper = ViewDataPropertyHelper.GetViewDataProperties <FullPocoController>();

            var controllerContext = new ControllerContext();
            var controller        = new FullPocoController
            {
                CustomControllerContext = controllerContext
            };

            var gotViewData = helper.ViewDataGetter(controller);

            Assert.NotNull(gotViewData);
            Assert.Same(gotViewData, controller.CustomViewData);

            MyApplication.StartsFrom <DefaultStartup>();
        }
 public static ViewDataDictionary GetViewData(this ComponentTestContext testContext)
 => testContext.ComponentAs <Controller>()?.ViewData
 ?? testContext.ComponentAs <ViewComponent>()?.ViewData
 ?? ViewDataPropertyHelper
 .GetViewDataProperties(testContext.Component.GetType())
 .ViewDataGetter(testContext.Component);