10
10
using Microsoft . AspNet . Mvc . ModelBinding ;
11
11
using Microsoft . AspNet . Mvc . Razor ;
12
12
using Microsoft . AspNet . Mvc . Rendering ;
13
+ using Microsoft . AspNet . PipelineCore ;
13
14
using Microsoft . AspNet . Razor . Runtime . TagHelpers ;
14
15
using Microsoft . AspNet . Routing ;
15
16
using Moq ;
@@ -41,8 +42,8 @@ public async Task ProcessAsync_GeneratesExpectedOutput()
41
42
var htmlGenerator = new TestableHtmlGenerator ( metadataProvider ) ;
42
43
Model model = null ;
43
44
var viewContext = TestableHtmlGenerator . GetViewContext ( model , htmlGenerator , metadataProvider ) ;
44
- var activator = new DefaultTagHelperActivator ( ) ;
45
- activator . Activate ( validationSummaryTagHelper , viewContext ) ;
45
+ validationSummaryTagHelper . ViewContext = viewContext ;
46
+ validationSummaryTagHelper . Generator = htmlGenerator ;
46
47
47
48
// Act
48
49
await validationSummaryTagHelper . ProcessAsync ( tagHelperContext , output ) ;
@@ -76,8 +77,8 @@ public async Task ProcessAsync_CallsIntoGenerateValidationSummaryWithExpectedPar
76
77
. Setup ( mock => mock . GenerateValidationSummary ( expectedViewContext , true , null , null , null ) )
77
78
. Returns ( new TagBuilder ( "div" ) )
78
79
. Verifiable ( ) ;
79
-
80
- SetViewContextAndGenerator ( validationSummaryTagHelper , expectedViewContext , generator . Object ) ;
80
+ validationSummaryTagHelper . ViewContext = expectedViewContext ;
81
+ validationSummaryTagHelper . Generator = generator . Object ;
81
82
82
83
// Act & Assert
83
84
await validationSummaryTagHelper . ProcessAsync ( context : null , output : output ) ;
@@ -107,29 +108,33 @@ public async Task ProcessAsync_MergesTagBuilderFromGenerateValidationSummary()
107
108
108
109
tagBuilder . Attributes . Add ( "data-foo" , "bar" ) ;
109
110
tagBuilder . Attributes . Add ( "data-hello" , "world" ) ;
111
+ tagBuilder . Attributes . Add ( "anything" , "something" ) ;
110
112
111
- var expectedViewContext = CreateViewContext ( ) ;
112
113
var generator = new Mock < IHtmlGenerator > ( MockBehavior . Strict ) ;
113
- var setup = generator
114
- . Setup ( mock => mock . GenerateValidationSummary ( It . IsAny < ViewContext > ( ) ,
114
+ generator
115
+ . Setup ( mock => mock . GenerateValidationSummary (
116
+ It . IsAny < ViewContext > ( ) ,
115
117
It . IsAny < bool > ( ) ,
116
118
It . IsAny < string > ( ) ,
117
119
It . IsAny < string > ( ) ,
118
120
It . IsAny < object > ( ) ) )
119
121
. Returns ( tagBuilder ) ;
120
-
121
- SetViewContextAndGenerator ( validationSummaryTagHelper , expectedViewContext , generator . Object ) ;
122
+ var viewContext = CreateViewContext ( ) ;
123
+ validationSummaryTagHelper . ViewContext = viewContext ;
124
+ validationSummaryTagHelper . Generator = generator . Object ;
122
125
123
126
// Act
124
127
await validationSummaryTagHelper . ProcessAsync ( context : null , output : output ) ;
125
128
126
129
// Assert
127
130
Assert . Equal ( output . TagName , "div" ) ;
128
- Assert . Equal ( 2 , output . Attributes . Count ) ;
131
+ Assert . Equal ( 3 , output . Attributes . Count ) ;
129
132
var attribute = Assert . Single ( output . Attributes , kvp => kvp . Key . Equals ( "data-foo" ) ) ;
130
133
Assert . Equal ( "bar" , attribute . Value ) ;
131
134
attribute = Assert . Single ( output . Attributes , kvp => kvp . Key . Equals ( "data-hello" ) ) ;
132
135
Assert . Equal ( "world" , attribute . Value ) ;
136
+ attribute = Assert . Single ( output . Attributes , kvp => kvp . Key . Equals ( "anything" ) ) ;
137
+ Assert . Equal ( "something" , attribute . Value ) ;
133
138
Assert . Equal ( "Content of validation summaryNew HTML" , output . Content ) ;
134
139
}
135
140
@@ -148,10 +153,10 @@ public async Task ProcessAsync_DoesNothingIfNullOrEmptyValidationSummaryValue(st
148
153
attributes : new Dictionary < string , string > ( ) ,
149
154
content : "Content of validation message" ) ;
150
155
151
- var expectedViewContext = CreateViewContext ( ) ;
152
156
var generator = new Mock < IHtmlGenerator > ( MockBehavior . Strict ) ;
153
-
154
- SetViewContextAndGenerator ( validationSummaryTagHelper , expectedViewContext , generator . Object ) ;
157
+ var viewContext = CreateViewContext ( ) ;
158
+ validationSummaryTagHelper . ViewContext = viewContext ;
159
+ validationSummaryTagHelper . Generator = generator . Object ;
155
160
156
161
// Act
157
162
await validationSummaryTagHelper . ProcessAsync ( context : null , output : output ) ;
@@ -183,18 +188,19 @@ public async Task ProcessAsync_GeneratesValidationSummaryWhenNotNone_IgnoresCase
183
188
InnerHtml = "New HTML"
184
189
} ;
185
190
186
- var expectedViewContext = CreateViewContext ( ) ;
187
- var generator = new Mock < IHtmlGenerator > ( MockBehavior . Loose ) ;
191
+ var generator = new Mock < IHtmlGenerator > ( ) ;
188
192
generator
189
- . Setup ( mock => mock . GenerateValidationSummary ( It . IsAny < ViewContext > ( ) ,
193
+ . Setup ( mock => mock . GenerateValidationSummary (
194
+ It . IsAny < ViewContext > ( ) ,
190
195
It . IsAny < bool > ( ) ,
191
196
It . IsAny < string > ( ) ,
192
197
It . IsAny < string > ( ) ,
193
198
It . IsAny < object > ( ) ) )
194
199
. Returns ( tagBuilder )
195
200
. Verifiable ( ) ;
196
-
197
- SetViewContextAndGenerator ( validationSummaryTagHelper , expectedViewContext , generator . Object ) ;
201
+ var viewContext = CreateViewContext ( ) ;
202
+ validationSummaryTagHelper . ViewContext = viewContext ;
203
+ validationSummaryTagHelper . Generator = generator . Object ;
198
204
199
205
// Act
200
206
await validationSummaryTagHelper . ProcessAsync ( context : null , output : output ) ;
@@ -225,14 +231,7 @@ public async Task ProcessAsync_DoesNotGenerateValidationSummaryWhenNone_IgnoresC
225
231
InnerHtml = "New HTML"
226
232
} ;
227
233
228
- var generator = new Mock < IHtmlGenerator > ( MockBehavior . Loose ) ;
229
- generator
230
- . Setup ( mock => mock . GenerateValidationSummary ( It . IsAny < ViewContext > ( ) ,
231
- It . IsAny < bool > ( ) ,
232
- It . IsAny < string > ( ) ,
233
- It . IsAny < string > ( ) ,
234
- It . IsAny < object > ( ) ) )
235
- . Throws ( new Exception ( "Shouldn't be called." ) ) ;
234
+ var generator = new Mock < IHtmlGenerator > ( MockBehavior . Strict ) ;
236
235
237
236
// Act
238
237
await validationSummaryTagHelper . ProcessAsync ( context : null , output : output ) ;
@@ -260,10 +259,8 @@ public async Task ProcessAsync_ThrowsWhenInvalidValidationSummaryValue()
260
259
"'All', 'ModelOnly' and 'None'." ;
261
260
262
261
// Act
263
- var ex = await Assert . ThrowsAsync < InvalidOperationException > ( async ( ) =>
264
- {
265
- await validationSummaryTagHelper . ProcessAsync ( context : null , output : output ) ;
266
- } ) ;
262
+ var ex = await Assert . ThrowsAsync < InvalidOperationException > ( ( ) =>
263
+ validationSummaryTagHelper . ProcessAsync ( context : null , output : output ) ) ;
267
264
268
265
// Assert
269
266
Assert . Equal ( expectedMessage , ex . Message ) ;
@@ -272,31 +269,18 @@ public async Task ProcessAsync_ThrowsWhenInvalidValidationSummaryValue()
272
269
private static ViewContext CreateViewContext ( )
273
270
{
274
271
var actionContext = new ActionContext (
275
- new Mock < HttpContext > ( ) . Object ,
272
+ new DefaultHttpContext ( ) ,
276
273
new RouteData ( ) ,
277
274
new ActionDescriptor ( ) ) ;
278
275
279
276
return new ViewContext (
280
277
actionContext ,
281
278
Mock . Of < IView > ( ) ,
282
279
new ViewDataDictionary (
283
- new DataAnnotationsModelMetadataProvider ( ) ) ,
284
- new StringWriter ( ) ) ;
280
+ new EmptyModelMetadataProvider ( ) ) ,
281
+ TextWriter . Null ) ;
285
282
}
286
283
287
- private static void SetViewContextAndGenerator ( ITagHelper tagHelper ,
288
- ViewContext viewContext ,
289
- IHtmlGenerator generator )
290
- {
291
- var tagHelperType = tagHelper . GetType ( ) ;
292
-
293
- tagHelperType . GetProperty ( "ViewContext" , BindingFlags . NonPublic | BindingFlags . Instance )
294
- . SetValue ( tagHelper , viewContext ) ;
295
- tagHelperType . GetProperty ( "Generator" , BindingFlags . NonPublic | BindingFlags . Instance )
296
- . SetValue ( tagHelper , generator ) ;
297
- }
298
-
299
-
300
284
private class Model
301
285
{
302
286
public string Text { get ; set ; }
0 commit comments