Example #1
0
        public void SetViewItem() {
            // Arrange
            ViewUserControl vuc = new ViewUserControl();
            object viewItem = new object();
            vuc.ViewData = new ViewDataDictionary(viewItem);

            // Act
            vuc.ViewData.Model = viewItem;
            object newViewItem = vuc.ViewData.Model;

            // Assert
            Assert.AreSame(viewItem, newViewItem);
        }
Example #2
0
        public void ModelProperty() {
            // Arrange
            object model = new object();
            ViewDataDictionary viewData = new ViewDataDictionary(model);
            ViewUserControl viewUserControl = new ViewUserControl();
            viewUserControl.ViewData = viewData;

            // Act
            object viewPageModel = viewUserControl.Model;

            // Assert
            Assert.AreEqual(model, viewPageModel);
            Assert.AreEqual(model, viewUserControl.ViewData.Model);
        }
Example #3
0
        public void SetViewItemOnBaseClassPropagatesToDerivedClass() {
            // Arrange
            ViewUserControl<object> vucInt = new ViewUserControl<object>();
            ViewUserControl vuc = vucInt;
            vuc.ViewData = new ViewDataDictionary();
            object o = new object();

            // Act
            vuc.ViewData.Model = o;

            // Assert
            Assert.AreEqual(o, vucInt.ViewData.Model);
            Assert.AreEqual(o, vuc.ViewData.Model);
        }
Example #4
0
        public void ModelPropertyStronglyTyped() {
            // Arrange
            FooModel model = new FooModel();
            ViewDataDictionary<FooModel> viewData = new ViewDataDictionary<FooModel>(model);
            ViewUserControl<FooModel> viewUserControl = new ViewUserControl<FooModel>();
            viewUserControl.ViewData = viewData;

            // Act
            object viewPageModelObject = ((ViewUserControl)viewUserControl).Model;
            FooModel viewPageModelPerson = viewUserControl.Model;

            // Assert
            Assert.AreEqual(model, viewPageModelObject);
            Assert.AreEqual(model, viewPageModelPerson);
        }
        public void GetUrlHelperFromRegularPage()
        {
            // Arrange
            ViewUserControl vuc = new ViewUserControl();
            Page containerPage = new Page();
            containerPage.Controls.Add(vuc);

            // Assert
            Assert.Throws<InvalidOperationException>(
                delegate { UrlHelper foo = vuc.Url; },
                "A ViewUserControl can be used only in pages that derive from ViewPage or ViewPage<TModel>.");
        }
        public void GetUrlHelperFromViewPage()
        {
            // Arrange
            ViewUserControl vuc = new ViewUserControl();
            ViewPage containerPage = new ViewPage();
            containerPage.Controls.Add(vuc);
            RequestContext rc = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData());
            UrlHelper urlHelper = new UrlHelper(rc);
            containerPage.Url = urlHelper;

            // Assert
            Assert.Equal(vuc.Url, urlHelper);
        }
        public void GetHtmlHelperFromViewPage()
        {
            // Arrange
            ViewUserControl vuc = new ViewUserControl();
            ViewPage containerPage = new ViewPage();
            containerPage.Controls.Add(vuc);
            ViewContext vc = new Mock<ViewContext>().Object;
            vuc.ViewContext = vc;

            // Act
            HtmlHelper htmlHelper = vuc.Html;

            // Assert
            Assert.Equal(vc, htmlHelper.ViewContext);
            Assert.Equal(vuc, htmlHelper.ViewDataContainer);
        }
        public void GetViewDataFromViewUserControlWithViewDataKeyOnOuterControl()
        {
            // Arrange
            ViewPage p = new ViewPage();
            p.Controls.Add(new Control());
            ViewUserControl outerVuc = new ViewUserControl() { ViewDataKey = "SubData" };
            p.Controls[0].Controls.Add(outerVuc);
            outerVuc.Controls.Add(new Control());
            ViewUserControl vuc = new ViewUserControl();
            outerVuc.Controls[0].Controls.Add(vuc);

            p.ViewData = new ViewDataDictionary { { "FirstName", "Joe" }, { "LastName", "Schmoe" } };
            p.ViewData["SubData"] = new ViewDataDictionary { { "FirstName", "SubJoe" }, { "LastName", "SubSchmoe" } };

            // Act
            object firstName = vuc.ViewData.Eval("FirstName");
            object lastName = vuc.ViewData.Eval("LastName");

            // Assert
            Assert.Equal("SubJoe", firstName);
            Assert.Equal("SubSchmoe", lastName);
        }
        public void GetViewDataFromViewPageWithViewDataKeyPointingToViewDataDictionary()
        {
            // Arrange
            ViewDataDictionary vdd = new ViewDataDictionary()
            {
                { "Foo", "FooParent" },
                { "Bar", "BarParent" },
                {
                    "Child",
                    new ViewDataDictionary()
                    {
                        { "Foo", "FooChild" },
                        { "Bar", "BarChild" }
                    }
                    }
            };

            ViewPage p = new ViewPage();
            p.Controls.Add(new Control());
            ViewUserControl vuc = new ViewUserControl() { ViewDataKey = "Child" };
            p.Controls[0].Controls.Add(vuc);
            p.ViewData = vdd;

            // Act
            object oFoo = vuc.ViewData.Eval("Foo");
            object oBar = vuc.ViewData.Eval("Bar");

            // Assert
            Assert.Equal(vdd["Child"], vuc.ViewData);
            Assert.Equal("FooChild", oFoo);
            Assert.Equal("BarChild", oBar);
        }
        public void ViewBagProperty_PropagatesChangesToViewData()
        {
            // Arrange
            ViewPage containerPage = new ViewPage();
            ViewUserControl userControl = new ViewUserControl();
            containerPage.Controls.Add(userControl);
            userControl.ViewData["A"] = 1;

            // Act
            userControl.ViewBag.A = "foo";
            userControl.ViewBag.B = 2;

            // Assert
            Assert.Equal("foo", userControl.ViewData["A"]);
            Assert.Equal(2, userControl.ViewData["B"]);
        }
Example #11
0
        public void GetHtmlHelperFromRegularPage() {
            // Arrange
            ViewUserControl vuc = new ViewUserControl();
            Page containerPage = new Page();
            containerPage.Controls.Add(vuc);

            // Assert
            ExceptionHelper.ExpectException<InvalidOperationException>(
                 delegate {
                     HtmlHelper foo = vuc.Html;
                 },
                 "A ViewUserControl can be used only in pages that derive from ViewPage or ViewPage<TViewItem>.");
        }
Example #12
0
        public void GetViewDataFromViewPage() {
            // Arrange
            ViewPage p = new ViewPage();
            p.Controls.Add(new Control());
            ViewUserControl vuc = new ViewUserControl();
            p.Controls[0].Controls.Add(vuc);
            p.ViewData = new ViewDataDictionary { { "FirstName", "Joe" }, { "LastName", "Schmoe" } };

            // Act
            object firstName = vuc.ViewData.Eval("FirstName");
            object lastName = vuc.ViewData.Eval("LastName");

            // Assert
            Assert.AreEqual("Joe", firstName);
            Assert.AreEqual("Schmoe", lastName);
        }
Example #13
0
        public void GetViewDataWhenNoPageSetThrows() {
            ViewUserControl vuc = new ViewUserControl();
            vuc.AppRelativeVirtualPath = "~/Foo.ascx";

            ExceptionHelper.ExpectException<InvalidOperationException>(
                delegate {
                    var foo = vuc.ViewData["Foo"];
                },
                "The ViewUserControl '~/Foo.ascx' cannot find an IViewDataContainer object. The ViewUserControl must be inside a ViewPage, a ViewMasterPage, or another ViewUserControl.");
        }
        public void ViewBagProperty_ReflectsViewData()
        {
            // Arrange
            ViewPage containerPage = new ViewPage();
            ViewUserControl userControl = new ViewUserControl();
            containerPage.Controls.Add(userControl);
            userControl.ViewData["A"] = 1;

            // Act & Assert
            Assert.NotNull(userControl.ViewBag);
            Assert.Equal(1, userControl.ViewBag.A);
        }
        public void SetViewItemToWrongTypeThrows()
        {
            // Arrange
            ViewUserControl<string> vucString = new ViewUserControl<string>();
            vucString.ViewData = new ViewDataDictionary<string>();
            ViewUserControl vuc = vucString;

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                delegate { vuc.ViewData.Model = 50; },
                "The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'System.String'.");
        }
        public void ViewBagProperty_ReflectsNewViewDataInstance()
        {
            // Arrange
            ViewPage containerPage = new ViewPage();
            ViewUserControl userControl = new ViewUserControl();
            containerPage.Controls.Add(userControl);
            userControl.ViewData["A"] = 1;
            userControl.ViewData = new ViewDataDictionary() { { "A", "bar" } };

            // Act & Assert
            Assert.Equal("bar", userControl.ViewBag.A);
        }
        public void GetViewDataWhenRegularPageSetThrows()
        {
            Page p = new Page();
            p.Controls.Add(new Control());
            ViewUserControl vuc = new ViewUserControl();
            p.Controls[0].Controls.Add(vuc);
            vuc.AppRelativeVirtualPath = "~/Foo.ascx";

            Assert.Throws<InvalidOperationException>(
                delegate { var foo = vuc.ViewData["Foo"]; },
                "The ViewUserControl '~/Foo.ascx' cannot find an IViewDataContainer object. The ViewUserControl must be inside a ViewPage, a ViewMasterPage, or another ViewUserControl.");
        }
 public MockViewUserControlContainerPage(ViewUserControl userControl)
 {
     Controls.Add(userControl);
 }
			public PageHolderContainerPage(ViewUserControl userControl)
			{
				_userControl = userControl;
			}