forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderPartialExtensionsTest.cs
More file actions
126 lines (105 loc) · 5.25 KB
/
Copy pathRenderPartialExtensionsTest.cs
File metadata and controls
126 lines (105 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Mvc.Html.Test
{
[Xunit.Collection("Uses ScopeStorage or ViewEngines.Engines")]
public class RenderPartialExtensionsTest
{
[Fact]
public void RenderPartialWithViewName()
{
// Arrange
SpyHtmlHelper helper = SpyHtmlHelper.Create();
// Act
helper.RenderPartial("partial-view");
// Assert
Assert.Equal("partial-view", helper.RenderPartialInternal_PartialViewName);
Assert.Same(helper.ViewData, helper.RenderPartialInternal_ViewData);
Assert.Null(helper.RenderPartialInternal_Model);
Assert.Same(helper.ViewContext.Writer, helper.RenderPartialInternal_Writer);
Assert.Same(ViewEngines.Engines, helper.RenderPartialInternal_ViewEngineCollection);
}
[Fact]
public void RenderPartialWithViewNameAndViewData()
{
// Arrange
SpyHtmlHelper helper = SpyHtmlHelper.Create();
ViewDataDictionary viewData = new ViewDataDictionary();
// Act
helper.RenderPartial("partial-view", viewData);
// Assert
Assert.Equal("partial-view", helper.RenderPartialInternal_PartialViewName);
Assert.Same(viewData, helper.RenderPartialInternal_ViewData);
Assert.Null(helper.RenderPartialInternal_Model);
Assert.Same(helper.ViewContext.Writer, helper.RenderPartialInternal_Writer);
Assert.Same(ViewEngines.Engines, helper.RenderPartialInternal_ViewEngineCollection);
}
[Fact]
public void RenderPartialWithViewNameAndModel()
{
// Arrange
SpyHtmlHelper helper = SpyHtmlHelper.Create();
object model = new object();
// Act
helper.RenderPartial("partial-view", model);
// Assert
Assert.Equal("partial-view", helper.RenderPartialInternal_PartialViewName);
Assert.Same(helper.ViewData, helper.RenderPartialInternal_ViewData);
Assert.Same(model, helper.RenderPartialInternal_Model);
Assert.Same(helper.ViewContext.Writer, helper.RenderPartialInternal_Writer);
Assert.Same(ViewEngines.Engines, helper.RenderPartialInternal_ViewEngineCollection);
}
[Fact]
public void RenderPartialWithViewNameAndModelAndViewData()
{
// Arrange
SpyHtmlHelper helper = SpyHtmlHelper.Create();
object model = new object();
ViewDataDictionary viewData = new ViewDataDictionary();
// Act
helper.RenderPartial("partial-view", model, viewData);
// Assert
Assert.Equal("partial-view", helper.RenderPartialInternal_PartialViewName);
Assert.Same(viewData, helper.RenderPartialInternal_ViewData);
Assert.Same(model, helper.RenderPartialInternal_Model);
Assert.Same(helper.ViewContext.Writer, helper.RenderPartialInternal_Writer);
Assert.Same(ViewEngines.Engines, helper.RenderPartialInternal_ViewEngineCollection);
}
internal class SpyHtmlHelper : HtmlHelper
{
public string RenderPartialInternal_PartialViewName;
public ViewDataDictionary RenderPartialInternal_ViewData;
public object RenderPartialInternal_Model;
public TextWriter RenderPartialInternal_Writer;
public ViewEngineCollection RenderPartialInternal_ViewEngineCollection;
SpyHtmlHelper(ViewContext viewContext, IViewDataContainer viewDataContainer)
: base(viewContext, viewDataContainer)
{
}
public static SpyHtmlHelper Create()
{
ViewDataDictionary viewData = new ViewDataDictionary();
Mock<ViewContext> mockViewContext = new Mock<ViewContext>();
mockViewContext.Setup(c => c.HttpContext.Response.Output).Throws(new Exception("Response.Output should never be called."));
mockViewContext.Setup(c => c.ViewData).Returns(viewData);
mockViewContext.Setup(c => c.Writer).Returns(new StringWriter());
Mock<IViewDataContainer> container = new Mock<IViewDataContainer>();
container.Setup(c => c.ViewData).Returns(viewData);
return new SpyHtmlHelper(mockViewContext.Object, container.Object);
}
internal override void RenderPartialInternal(string partialViewName, ViewDataDictionary viewData, object model,
TextWriter writer, ViewEngineCollection viewEngineCollection)
{
RenderPartialInternal_PartialViewName = partialViewName;
RenderPartialInternal_ViewData = viewData;
RenderPartialInternal_Model = model;
RenderPartialInternal_Writer = writer;
RenderPartialInternal_ViewEngineCollection = viewEngineCollection;
writer.Write("This is the result of the view");
}
}
}
}