1
+ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2
+ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
+
4
+ using System . Collections . Generic ;
5
+ using System . IO ;
6
+ using System . Reflection ;
7
+ using Microsoft . AspNet . Http ;
8
+ using Microsoft . AspNet . Mvc . ModelBinding ;
9
+ using Microsoft . AspNet . Mvc . Rendering ;
10
+ using Microsoft . AspNet . Razor . Runtime . TagHelpers ;
11
+ using Microsoft . AspNet . Routing ;
12
+ using Moq ;
13
+ using Xunit ;
14
+
15
+ namespace Microsoft . AspNet . Mvc . TagHelpers
16
+ {
17
+ public class ValidationMessageTagHelperTest
18
+ {
19
+ [ Fact ]
20
+ public void Process_CallsIntoGenerateValidationMessageWithExpectedParameters ( )
21
+ {
22
+ // Arrange
23
+ var validationMessageTagHelper = new ValidationMessageTagHelper
24
+ {
25
+ For = CreateModelExpression ( "Hello" )
26
+ } ;
27
+ var called = false ;
28
+ var output = new TagHelperOutput (
29
+ "span" ,
30
+ attributes : new Dictionary < string , string > ( ) ,
31
+ content : "Content of validation message" ) ;
32
+
33
+ var expectedViewContext = CreateViewContext ( ) ;
34
+ var generator = new Mock < IHtmlGenerator > ( ) ;
35
+ generator . Setup ( mock =>
36
+ mock . GenerateValidationMessage ( It . IsAny < ViewContext > ( ) ,
37
+ It . IsAny < string > ( ) ,
38
+ It . IsAny < string > ( ) ,
39
+ It . IsAny < string > ( ) ,
40
+ It . IsAny < object > ( ) ) )
41
+ . Callback < ViewContext , string , string , string , object > (
42
+ ( viewContext , name , message , tag , htmlAttributes ) =>
43
+ {
44
+ called = true ;
45
+
46
+ Assert . Same ( expectedViewContext , viewContext ) ;
47
+ Assert . Equal ( "Hello" , name ) ;
48
+ Assert . Equal ( "Content of validation message" , message ) ;
49
+ Assert . Equal ( "span" , tag ) ;
50
+ Assert . Null ( htmlAttributes ) ;
51
+ } )
52
+ . Returns ( new TagBuilder ( "span" ) ) ;
53
+
54
+ SetViewContextAndGenerator ( validationMessageTagHelper , expectedViewContext , generator . Object ) ;
55
+
56
+ // Act & Assert
57
+ validationMessageTagHelper . Process ( context : null , output : output ) ;
58
+
59
+ Assert . True ( called ) ;
60
+ }
61
+
62
+ [ Fact ]
63
+ public void Process_MergesTagBuilderFromGenerateValidationMessage ( )
64
+ {
65
+ // Arrange
66
+ var validationMessageTagHelper = new ValidationMessageTagHelper
67
+ {
68
+ For = CreateModelExpression ( "Hello" )
69
+ } ;
70
+ var output = new TagHelperOutput (
71
+ "span" ,
72
+ attributes : new Dictionary < string , string > ( ) ,
73
+ content : "Content of validation message" ) ;
74
+ var tagBuilder = new TagBuilder ( "span2" )
75
+ {
76
+ InnerHtml = "New HTML"
77
+ } ;
78
+
79
+ tagBuilder . Attributes . Add ( "data-foo" , "bar" ) ;
80
+ tagBuilder . Attributes . Add ( "data-hello" , "world" ) ;
81
+
82
+ var expectedViewContext = CreateViewContext ( ) ;
83
+ var generator = new Mock < IHtmlGenerator > ( MockBehavior . Strict ) ;
84
+ generator . Setup ( mock =>
85
+ mock . GenerateValidationMessage ( It . IsAny < ViewContext > ( ) ,
86
+ It . IsAny < string > ( ) ,
87
+ It . IsAny < string > ( ) ,
88
+ It . IsAny < string > ( ) ,
89
+ It . IsAny < object > ( ) ) )
90
+ . Returns ( tagBuilder ) ;
91
+
92
+ SetViewContextAndGenerator ( validationMessageTagHelper , expectedViewContext , generator . Object ) ;
93
+
94
+ // Act
95
+ validationMessageTagHelper . Process ( context : null , output : output ) ;
96
+
97
+ // Assert
98
+ Assert . Equal ( output . TagName , "span2" ) ;
99
+ Assert . Equal ( 2 , output . Attributes . Count ) ;
100
+ var attribute = Assert . Single ( output . Attributes , kvp => kvp . Key . Equals ( "data-foo" ) ) ;
101
+ Assert . Equal ( "bar" , attribute . Value ) ;
102
+ attribute = Assert . Single ( output . Attributes , kvp => kvp . Key . Equals ( "data-hello" ) ) ;
103
+ Assert . Equal ( "world" , attribute . Value ) ;
104
+ Assert . Equal ( "New HTML" , output . Content ) ;
105
+ }
106
+
107
+ [ Fact ]
108
+ public void Process_DoesNothingIfNullFor ( )
109
+ {
110
+ // Arrange
111
+ var validationMessageTagHelper = new ValidationMessageTagHelper ( ) ;
112
+ var output = new TagHelperOutput (
113
+ "span" ,
114
+ attributes : new Dictionary < string , string > ( ) ,
115
+ content : "Content of validation message" ) ;
116
+
117
+ var expectedViewContext = CreateViewContext ( ) ;
118
+ var generator = new Mock < IHtmlGenerator > ( MockBehavior . Strict ) ;
119
+
120
+ SetViewContextAndGenerator ( validationMessageTagHelper , expectedViewContext , generator . Object ) ;
121
+
122
+ // Act & Assert
123
+ validationMessageTagHelper . Process ( context : null , output : output ) ;
124
+
125
+ Assert . Equal ( "span" , output . TagName ) ;
126
+ Assert . Empty ( output . Attributes ) ;
127
+ Assert . Equal ( "Content of validation message" , output . Content ) ;
128
+ }
129
+
130
+ private static ModelExpression CreateModelExpression ( string name )
131
+ {
132
+ return new ModelExpression (
133
+ name ,
134
+ new ModelMetadata (
135
+ new Mock < IModelMetadataProvider > ( ) . Object ,
136
+ containerType : null ,
137
+ modelAccessor : null ,
138
+ modelType : typeof ( object ) ,
139
+ propertyName : string . Empty ) ) ;
140
+ }
141
+
142
+ private static ViewContext CreateViewContext ( )
143
+ {
144
+ var actionContext = new ActionContext (
145
+ new Mock < HttpContext > ( ) . Object ,
146
+ new RouteData ( ) ,
147
+ new ActionDescriptor ( ) ) ;
148
+
149
+ return new ViewContext (
150
+ actionContext ,
151
+ Mock . Of < IView > ( ) ,
152
+ new ViewDataDictionary (
153
+ new DataAnnotationsModelMetadataProvider ( ) ) ,
154
+ new StringWriter ( ) ) ;
155
+ }
156
+
157
+ private static void SetViewContextAndGenerator ( ITagHelper tagHelper ,
158
+ ViewContext viewContext ,
159
+ IHtmlGenerator generator )
160
+ {
161
+ var tagHelperType = tagHelper . GetType ( ) ;
162
+
163
+ tagHelperType . GetProperty ( "ViewContext" , BindingFlags . NonPublic | BindingFlags . Instance )
164
+ . SetValue ( tagHelper , viewContext ) ;
165
+ tagHelperType . GetProperty ( "Generator" , BindingFlags . NonPublic | BindingFlags . Instance )
166
+ . SetValue ( tagHelper , generator ) ;
167
+ }
168
+ }
169
+ }
0 commit comments