Skip to content

Commit

Permalink
Add a few more Display() and Editor() tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dougbu committed Oct 31, 2014
1 parent bb7fad6 commit dbffb8f
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 0 deletions.
97 changes: 97 additions & 0 deletions test/System.Web.Mvc.Test/Html/Test/DisplayExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,103 @@ public void UrlTemplate_AttributeAndHtmlEncodes(
Assert.Equal(expectedResult, displayForModelResult);
}

[Fact]
public void Display_FindsViewDataMember()
{
// Arrange
var model = new ObjectTemplateModel { Property1 = "Model string" };
var viewData = new ViewDataDictionary<ObjectTemplateModel>(model);
viewData["Property1"] = "ViewData string";
var html = MvcHelper.GetHtmlHelper(viewData);

var viewContext = Mock.Get(html.ViewContext);
viewContext.Setup(c => c.TempData).Returns(new TempDataDictionary());
viewContext.Setup(c => c.View).Returns(new DummyView());
viewContext.Setup(c => c.Writer).Returns(TextWriter.Null);

MvcHtmlString result;
using (new TemplateHelpersSafeScope())
{
// Act
result = html.Display("Property1");
}

// Assert
Assert.Equal("ViewData string", result.ToString());
}

[Fact]
public void DisplayFor_FindsModel()
{
var model = new ObjectTemplateModel { Property1 = "Model string" };
var viewData = new ViewDataDictionary<ObjectTemplateModel>(model);
viewData["Property1"] = "ViewData string";
var html = MvcHelper.GetHtmlHelper(viewData);

var viewContext = Mock.Get(html.ViewContext);
viewContext.Setup(c => c.TempData).Returns(new TempDataDictionary());
viewContext.Setup(c => c.View).Returns(new DummyView());
viewContext.Setup(c => c.Writer).Returns(TextWriter.Null);

MvcHtmlString result;
using (new TemplateHelpersSafeScope())
{
// Act
result = html.DisplayFor(m => m.Property1);
}

// Assert
Assert.Equal("Model string", result.ToString());
}

[Fact]
public void Display_FindsModel_IfNoViewDataMember()
{
// Arrange
var model = new ObjectTemplateModel { Property1 = "Model string" };
var viewData = new ViewDataDictionary<ObjectTemplateModel>(model);
var html = MvcHelper.GetHtmlHelper(viewData);

var viewContext = Mock.Get(html.ViewContext);
viewContext.Setup(c => c.TempData).Returns(new TempDataDictionary());
viewContext.Setup(c => c.View).Returns(new DummyView());
viewContext.Setup(c => c.Writer).Returns(TextWriter.Null);

MvcHtmlString result;
using (new TemplateHelpersSafeScope())
{
// Act
result = html.Display("Property1");
}

// Assert
Assert.Equal("Model string", result.ToString());
}

[Fact]
public void DisplayFor_FindsModel_EvenIfNull()
{
var model = new ObjectTemplateModel();
var viewData = new ViewDataDictionary<ObjectTemplateModel>(model);
viewData["Property1"] = "ViewData string";
var html = MvcHelper.GetHtmlHelper(viewData);

var viewContext = Mock.Get(html.ViewContext);
viewContext.Setup(c => c.TempData).Returns(new TempDataDictionary());
viewContext.Setup(c => c.View).Returns(new DummyView());
viewContext.Setup(c => c.Writer).Returns(TextWriter.Null);

MvcHtmlString result;
using (new TemplateHelpersSafeScope())
{
// Act
result = html.DisplayFor(m => m.Property1);
}

// Assert
Assert.Empty(result.ToString());
}

private class ObjectTemplateModel
{
public string Property1 { get; set; }
Expand Down
105 changes: 105 additions & 0 deletions test/System.Web.Mvc.Test/Html/Test/EditorExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,111 @@ public void StringTemplate_AttributeEncodesText(
Assert.Equal(expectedResult, editorForModelResult);
}

[Fact]
public void Editor_FindsViewDataMember()
{
// Arrange
var model = new ObjectTemplateModel { Property1 = "Model string" };
var viewData = new ViewDataDictionary<ObjectTemplateModel>(model);
viewData["Property1"] = "ViewData string";
var html = MvcHelper.GetHtmlHelper(viewData);

var viewContext = Mock.Get(html.ViewContext);
viewContext.Setup(c => c.TempData).Returns(new TempDataDictionary());
viewContext.Setup(c => c.View).Returns(new DummyView());
viewContext.Setup(c => c.Writer).Returns(TextWriter.Null);

MvcHtmlString result;
using (new TemplateHelpersSafeScope())
{
// Act
result = html.Editor("Property1");
}

// Assert
Assert.Equal(
"<input class=\"text-box single-line\" id=\"Property1\" name=\"Property1\" type=\"text\" value=\"ViewData string\" />",
result.ToString());
}

[Fact]
public void EditorFor_FindsModel()
{
var model = new ObjectTemplateModel { Property1 = "Model string" };
var viewData = new ViewDataDictionary<ObjectTemplateModel>(model);
viewData["Property1"] = "ViewData string";
var html = MvcHelper.GetHtmlHelper(viewData);

var viewContext = Mock.Get(html.ViewContext);
viewContext.Setup(c => c.TempData).Returns(new TempDataDictionary());
viewContext.Setup(c => c.View).Returns(new DummyView());
viewContext.Setup(c => c.Writer).Returns(TextWriter.Null);

MvcHtmlString result;
using (new TemplateHelpersSafeScope())
{
// Act
result = html.EditorFor(m => m.Property1);
}

// Assert
Assert.Equal(
"<input class=\"text-box single-line\" id=\"Property1\" name=\"Property1\" type=\"text\" value=\"Model string\" />",
result.ToString());
}

[Fact]
public void Editor_FindsModel_IfNoViewDataMember()
{
// Arrange
var model = new ObjectTemplateModel { Property1 = "Model string" };
var viewData = new ViewDataDictionary<ObjectTemplateModel>(model);
var html = MvcHelper.GetHtmlHelper(viewData);

var viewContext = Mock.Get(html.ViewContext);
viewContext.Setup(c => c.TempData).Returns(new TempDataDictionary());
viewContext.Setup(c => c.View).Returns(new DummyView());
viewContext.Setup(c => c.Writer).Returns(TextWriter.Null);

MvcHtmlString result;
using (new TemplateHelpersSafeScope())
{
// Act
result = html.Editor("Property1");
}

// Assert
Assert.Equal(
"<input class=\"text-box single-line\" id=\"Property1\" name=\"Property1\" type=\"text\" value=\"Model string\" />",
result.ToString());
}

[Fact]
public void EditorFor_FindsModel_EvenIfNull()
{
var model = new ObjectTemplateModel();
var viewData = new ViewDataDictionary<ObjectTemplateModel>(model);
viewData["Property1"] = "ViewData string";
var html = MvcHelper.GetHtmlHelper(viewData);

var viewContext = Mock.Get(html.ViewContext);
viewContext.Setup(c => c.TempData).Returns(new TempDataDictionary());
viewContext.Setup(c => c.View).Returns(new DummyView());
viewContext.Setup(c => c.Writer).Returns(TextWriter.Null);

MvcHtmlString result;
using (new TemplateHelpersSafeScope())
{
// Act
result = html.EditorFor(m => m.Property1);
}

// Assert
Assert.Equal(
"<input class=\"text-box single-line\" id=\"Property1\" name=\"Property1\" type=\"text\" value=\"\" />",
result.ToString());
}

private class ObjectTemplateModel
{
public string Property1 { get; set; }
Expand Down

0 comments on commit dbffb8f

Please sign in to comment.