public void ValueHelpersWithErrorsGetValueFromModelState() { // Arrange ViewDataDictionary <FooBarModel> viewDataWithErrors = new ViewDataDictionary <FooBarModel> { { "foo", "ViewDataFoo" } }; viewDataWithErrors.Model = new FooBarModel() { foo = "ViewItemFoo", bar = "ViewItemBar" }; viewDataWithErrors.TemplateInfo.HtmlFieldPrefix = "FieldPrefix"; ModelState modelStateFoo = new ModelState(); modelStateFoo.Value = HtmlHelperTest.GetValueProviderResult(new string[] { "AttemptedValueFoo" }, "AttemptedValueFoo"); viewDataWithErrors.ModelState["FieldPrefix.foo"] = modelStateFoo; ModelState modelStateFooBar = new ModelState(); modelStateFooBar.Value = HtmlHelperTest.GetValueProviderResult(new string[] { "AttemptedValueFooBar" }, "AttemptedValueFooBar"); viewDataWithErrors.ModelState["FieldPrefix"] = modelStateFooBar; HtmlHelper <FooBarModel> helper = MvcHelper.GetHtmlHelper(viewDataWithErrors); // Act & Assert Assert.Equal("AttemptedValueFoo", helper.Value("foo").ToHtmlString()); Assert.Equal("AttemptedValueFoo", helper.ValueFor(m => m.foo).ToHtmlString()); Assert.Equal("AttemptedValueFooBar", helper.ValueForModel().ToHtmlString()); }
public void SelectHelpersUseCurrentCultureToConvertValues() { // Arrange HtmlHelper defaultValueHelper = HtmlHelperTest.GetHtmlHelper(new ViewDataDictionary { { "foo", new [] { new DateTime(1900, 1, 1, 0, 0, 1) } }, { "bar", new DateTime(1900, 1, 1, 0, 0, 1) } }); HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(); SelectList selectList = new SelectList(GetSampleCultureAnonymousObjects(), "Date", "FullWord", new DateTime(1900, 1, 1, 0, 0, 0)); var tests = new[] { // DropDownList(name, selectList, optionLabel) new { Html = @"<select id=""foo"" name=""foo""><option selected=""selected"" value=""1900/01/01 12:00:00 AM"">Alpha</option> <option value=""1900/01/01 12:00:01 AM"">Bravo</option> <option value=""1900/01/01 12:00:02 AM"">Charlie</option> </select>", Action = new GenericDelegate <string>(() => helper.DropDownList("foo", selectList, (string)null)) }, // DropDownList(name, selectList, optionLabel) (With default value selected from ViewData) new { Html = @"<select id=""bar"" name=""bar""><option value=""1900/01/01 12:00:00 AM"">Alpha</option> <option selected=""selected"" value=""1900/01/01 12:00:01 AM"">Bravo</option> <option value=""1900/01/01 12:00:02 AM"">Charlie</option> </select>", Action = new GenericDelegate <string>(() => defaultValueHelper.DropDownList("bar", selectList, (string)null)) }, // ListBox(name, selectList) new { Html = @"<select id=""foo"" multiple=""multiple"" name=""foo""><option selected=""selected"" value=""1900/01/01 12:00:00 AM"">Alpha</option> <option value=""1900/01/01 12:00:01 AM"">Bravo</option> <option value=""1900/01/01 12:00:02 AM"">Charlie</option> </select>", Action = new GenericDelegate <string>(() => helper.ListBox("foo", selectList)) }, // ListBox(name, selectList) (With default value selected from ViewData) new { Html = @"<select id=""foo"" multiple=""multiple"" name=""foo""><option value=""1900/01/01 12:00:00 AM"">Alpha</option> <option selected=""selected"" value=""1900/01/01 12:00:01 AM"">Bravo</option> <option value=""1900/01/01 12:00:02 AM"">Charlie</option> </select>", Action = new GenericDelegate <string>(() => defaultValueHelper.ListBox("foo", selectList)) } }; // Act && Assert using (HtmlHelperTest.ReplaceCulture("en-ZA", "en-US")) { foreach (var test in tests) { Assert.AreEqual(test.Html, test.Action()); } } }
public void ActionLinkParametersNeedEscaping() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(); // Act string html = htmlHelper.ActionLink("linktext<&>\"", "new action<&>\""); // Assert Assert.AreEqual <string>(@"<a href=""" + AppPathModifier + @"/app/home/new%20action%3C&%3E%22"">linktext<&>"</a>", html); }
public void ValidationMessageReturnsWithCustomMessage() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(GetViewDataWithModelErrors()); // Act string html = htmlHelper.ValidationMessage("foo", "bar error"); // Assert Assert.AreEqual(@"<span class=""field-validation-error"">bar error</span>", html); }
public void TextBoxWithImplicitValue() { // Arrange HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetTextBoxViewData()); // Act string html = helper.TextBox("foo"); // Assert Assert.AreEqual(@"<input id=""foo"" name=""foo"" type=""text"" value=""ViewDataFoo"" />", html); }
public void ValidationMessageReturnsGenericMessageInsteadOfExceptionText() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(GetViewDataWithModelErrors()); // Act string html = htmlHelper.ValidationMessage("quux"); // Assert Assert.AreEqual(@"<span class=""field-validation-error"">The value 'quuxValue' is invalid.</span>", html); }
public void ValidationMessageReturnsWithObjectAttributes() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(GetViewDataWithModelErrors()); // Act string html = htmlHelper.ValidationMessage("foo", new { bar = "bar" }); // Assert Assert.AreEqual(@"<span bar=""bar"" class=""field-validation-error"">foo error <1></span>", html); }
public void ValidationMessageThrowsIfModelNameIsNull() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(); // Act & Assert ExceptionHelper.ExpectArgumentNullException( delegate { htmlHelper.ValidationMessage(null); }, "modelName"); }
public void ValidationSummaryWithNoErrorsReturnsNull() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(new ViewDataDictionary()); // Act string html = htmlHelper.ValidationSummary(); // Assert Assert.IsNull(html, "html should be null if there are no errors to report."); }
public void ActionLinkWithProtocol() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(); // Act string html = htmlHelper.ActionLink("linktext", "newaction", "home2", "https", "foo.bar.com", null /* fragment */, new { id = "someid" }, new { baz = "baz" }); // Assert Assert.AreEqual <string>(@"<a baz=""baz"" href=""https://foo.bar.com" + AppPathModifier + @"/app/home2/newaction/someid"">linktext</a>", html); }
public void ActionLinkDictionaryOverridesImplicitValues() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(); // Act string html = htmlHelper.ActionLink("linktext", "newaction", null, new { href = "http://foo.com" }); // Assert Assert.AreEqual <string>(@"<a href=""http://foo.com"">linktext</a>", html); }
public void ActionLinkWithNullHostname() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(); // Act string html = htmlHelper.ActionLink("linktext", "newaction", "home2", "https", null /* hostName */, "foo", new { id = "someid" }, new { baz = "baz" }); // Assert Assert.AreEqual <string>(@"<a baz=""baz"" href=""https://localhost" + AppPathModifier + @"/app/home2/newaction/someid#foo"">linktext</a>", html); }
public void ActionLinkWithNullProtocolNullHostNameAndNullFragment() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(); // Act string html = htmlHelper.ActionLink("linktext", "newaction", "home2", null /* protocol */, null /* hostName */, null /* fragment */, new { id = "someid" }, new { baz = "baz" }); // Assert Assert.AreEqual <string>(@"<a baz=""baz"" href=""" + AppPathModifier + @"/app/home2/newaction/someid"">linktext</a>", html); }
public void ActionLinkWithDictionary() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(); // Act string html = htmlHelper.ActionLink("linktext", "newaction", new RouteValueDictionary(new { Controller = "home2", id = "someid" }), new RouteValueDictionary(new { baz = "baz" })); // Assert Assert.AreEqual <string>(@"<a baz=""baz"" href=""" + AppPathModifier + @"/app/home2/newaction/someid"">linktext</a>", html); }
public void TextBoxWithImplicitValueAndAttributesObject() { // Arrange HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetTextBoxViewData()); // Act string html = helper.TextBox("foo", null, _attributesObjectDictionary); // Assert Assert.AreEqual(@"<input baz=""BazObjValue"" id=""foo"" name=""foo"" type=""text"" value=""ViewDataFoo"" />", html); }
public void TextBoxWithImplicitValueAndAttributesDictionaryReturnsEmptyValueIfNotFound() { // Arrange HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetTextBoxViewData()); // Act string html = helper.TextBox("keyNotFound", null, _attributesDictionary); // Assert Assert.AreEqual(@"<input baz=""BazValue"" id=""keyNotFound"" name=""keyNotFound"" type=""text"" value="""" />", html); }
public void ActionLinkWithActionNameAndValueDictionary() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(); // Act string html = htmlHelper.ActionLink("linktext", "newaction", new RouteValueDictionary(new { controller = "home2" })); // Assert Assert.AreEqual <string>(@"<a href=""" + AppPathModifier + @"/app/home2/newaction"">linktext</a>", html); }
public void ActionLinkWithNonDefaultPortAndSameProtocol() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(Uri.UriSchemeHttp, 32768); // Act string html = htmlHelper.ActionLink("linktext", "newaction", "home2", "http", "foo.bar.com", "foo", new { id = "someid" }, new { baz = "baz" }); // Assert Assert.AreEqual <string>(@"<a baz=""baz"" href=""http://foo.bar.com:32768" + AppPathModifier + @"/app/home2/newaction/someid#foo"">linktext</a>", html); }
public void ActionLinkWithControllerName() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(); // Act string html = htmlHelper.ActionLink("linktext", "newaction", "home2"); // Assert Assert.AreEqual <string>(@"<a href=""" + AppPathModifier + @"/app/home2/newaction"">linktext</a>", html); }
public void ActionLinkExplictValuesOverrideDictionary() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(); // Act string html = htmlHelper.ActionLink("linktext", "explicitAction", new { action = "dictionaryAction" }, null); // Assert Assert.AreEqual <string>(@"<a href=""" + AppPathModifier + @"/app/home/explicitAction"">linktext</a>", html); }
public void ValidationMessageWithModelStateAndNoErrors() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(GetViewDataWithModelErrors()); // Act string html = htmlHelper.ValidationMessage("baz"); // Assert Assert.IsNull(html, "html should be null if there are no errors"); }
public void RouteLinkWithProtocolAndFragment() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(); // Act string html = htmlHelper.RouteLink("linktext", "namedroute", "https", "foo.bar.com", "foo", new { Action = "newaction", Controller = "home2", id = "someid" }, new { baz = "baz" }); // Assert Assert.AreEqual <string>(@"<a baz=""baz"" href=""https://foo.bar.com" + AppPathModifier + @"/app/named/home2/newaction/someid#foo"">linktext</a>", html); }
public void ValidationMessageReturnsFirstError() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(GetViewDataWithModelErrors()); // Act string html = htmlHelper.ValidationMessage("foo"); // Assert Assert.AreEqual(@"<span class=""field-validation-error"">foo error <1></span>", html); }
public void RouteLinkWithRouteNameAndDefaults() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(); // Act string html = htmlHelper.RouteLink("linktext", "namedroute", new { Action = "newaction" }); // Assert Assert.AreEqual <string>(@"<a href=""" + AppPathModifier + @"/app/named/home/newaction"">linktext</a>", html); }
public void ValidationMessageReturnsNullForInvalidName() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(GetViewDataWithModelErrors()); // Act string html = htmlHelper.ValidationMessage("boo"); // Assert Assert.IsNull(html, "html should be null if name is invalid."); }
public void TextBoxWithDotReplacementForId() { // Arrange HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetTextBoxViewData()); // Act string html = helper.TextBox("foo.bar.baz", null); // Assert Assert.AreEqual(@"<input id=""foo_bar_baz"" name=""foo.bar.baz"" type=""text"" value="""" />", html); }
public void ValidationMessageReturnsWithCustomClassOverridesDefault() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(GetViewDataWithModelErrors()); // Act string html = htmlHelper.ValidationMessage("foo", new { @class = "my-custom-css-class" }); // Assert Assert.AreEqual(@"<span class=""my-custom-css-class"">foo error <1></span>", html); }
public void RouteLinkWithRouteNameAndObjectProperties() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(); // Act string html = htmlHelper.RouteLink("linktext", "namedroute", new { Action = "newaction", Controller = "home2", id = "someid" }, new { baz = "baz" }); // Assert Assert.AreEqual <string>(@"<a baz=""baz"" href=""" + AppPathModifier + @"/app/named/home2/newaction/someid"">linktext</a>", html); }
public void RouteLinkWithRouteNameAndDictionary() { // Arrange HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(); // Act string html = htmlHelper.RouteLink("linktext", "namedroute", new RouteValueDictionary(new { Action = "newaction", Controller = "home2", id = "someid" }), new RouteValueDictionary()); // Assert Assert.AreEqual <string>(@"<a href=""" + AppPathModifier + @"/app/named/home2/newaction/someid"">linktext</a>", html); }
public void TextBoxWithExplicitValueNull() { // Arrange HtmlHelper helper = HtmlHelperTest.GetHtmlHelper(GetTextBoxViewData()); // Act string html = helper.TextBox("foo", (string)null /* value */, (object)null); // Assert Assert.AreEqual(@"<input id=""foo"" name=""foo"" type=""text"" value=""ViewDataFoo"" />", html); }