4
4
using System . Collections . Generic ;
5
5
using System . IO ;
6
6
using System . Reflection ;
7
+ using System . Threading . Tasks ;
7
8
using Microsoft . AspNet . Http ;
8
9
using Microsoft . AspNet . Mvc . ModelBinding ;
10
+ using Microsoft . AspNet . Mvc . Razor ;
9
11
using Microsoft . AspNet . Mvc . Rendering ;
10
12
using Microsoft . AspNet . Razor . Runtime . TagHelpers ;
11
13
using Microsoft . AspNet . Routing ;
@@ -17,50 +19,93 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
17
19
public class ValidationMessageTagHelperTest
18
20
{
19
21
[ Fact ]
20
- public void Process_CallsIntoGenerateValidationMessageWithExpectedParameters ( )
22
+ public async Task ProcessAsync_GeneratesExpectedOutput ( )
23
+ {
24
+ // Arrange
25
+ var metadataProvider = new DataAnnotationsModelMetadataProvider ( ) ;
26
+ var modelExpression = CreateModelExpression ( "Name" ) ;
27
+ var validationMessageTagHelper = new ValidationMessageTagHelper
28
+ {
29
+ For = modelExpression
30
+ } ;
31
+
32
+ var tagHelperContext = new TagHelperContext (
33
+ allAttributes : new Dictionary < string , object >
34
+ {
35
+ { "id" , "myvalidationmessage" } ,
36
+ { "for" , modelExpression } ,
37
+ } ) ;
38
+ var output = new TagHelperOutput (
39
+ "original tag name" ,
40
+ attributes : new Dictionary < string , string >
41
+ {
42
+ { "id" , "myvalidationmessage" }
43
+ } ,
44
+ content : "Something" ) ;
45
+ var htmlGenerator = new TestableHtmlGenerator ( metadataProvider ) ;
46
+ var viewContext = TestableHtmlGenerator . GetViewContext ( model : null ,
47
+ htmlGenerator : htmlGenerator ,
48
+ metadataProvider : metadataProvider ) ;
49
+
50
+ var activator = new DefaultTagHelperActivator ( ) ;
51
+ activator . Activate ( validationMessageTagHelper , viewContext ) ;
52
+
53
+ // Act
54
+ await validationMessageTagHelper . ProcessAsync ( tagHelperContext , output ) ;
55
+
56
+ // Assert
57
+ Assert . Equal ( 4 , output . Attributes . Count ) ;
58
+ var attribute = Assert . Single ( output . Attributes , kvp => kvp . Key . Equals ( "id" ) ) ;
59
+ Assert . Equal ( "myvalidationmessage" , attribute . Value ) ;
60
+ attribute = Assert . Single ( output . Attributes , kvp => kvp . Key . Equals ( "class" ) ) ;
61
+ Assert . Equal ( "field-validation-valid" , attribute . Value ) ;
62
+ attribute = Assert . Single ( output . Attributes , kvp => kvp . Key . Equals ( "data-valmsg-for" ) ) ;
63
+ Assert . Equal ( "Name" , attribute . Value ) ;
64
+ attribute = Assert . Single ( output . Attributes , kvp => kvp . Key . Equals ( "data-valmsg-replace" ) ) ;
65
+ Assert . Equal ( "true" , attribute . Value ) ;
66
+ Assert . Equal ( "Something" , output . Content ) ;
67
+ Assert . Equal ( "original tag name" , output . TagName ) ;
68
+ }
69
+
70
+ [ Fact ]
71
+ public async Task ProcessAsync_CallsIntoGenerateValidationMessageWithExpectedParameters ( )
21
72
{
22
73
// Arrange
23
74
var validationMessageTagHelper = new ValidationMessageTagHelper
24
75
{
25
76
For = CreateModelExpression ( "Hello" )
26
77
} ;
27
- var called = false ;
28
78
var output = new TagHelperOutput (
29
79
"span" ,
30
80
attributes : new Dictionary < string , string > ( ) ,
31
81
content : "Content of validation message" ) ;
32
-
33
82
var expectedViewContext = CreateViewContext ( ) ;
34
83
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" ) ) ;
84
+ var setup = generator . Setup ( mock =>
85
+ mock . GenerateValidationMessage ( expectedViewContext ,
86
+ "Hello" ,
87
+ null ,
88
+ null ,
89
+ null ) ) ;
90
+ setup . Returns ( new TagBuilder ( "span" ) ) ;
91
+ setup . Verifiable ( ) ;
53
92
54
93
SetViewContextAndGenerator ( validationMessageTagHelper , expectedViewContext , generator . Object ) ;
55
94
56
95
// Act & Assert
57
- validationMessageTagHelper . Process ( context : null , output : output ) ;
96
+ await validationMessageTagHelper . ProcessAsync ( context : null , output : output ) ;
58
97
59
- Assert . True ( called ) ;
98
+ generator . Verify ( ) ;
99
+ Assert . Equal ( "span" , output . TagName ) ;
100
+ Assert . Empty ( output . Attributes ) ;
101
+ Assert . Equal ( "Content of validation message" , output . Content ) ;
60
102
}
61
103
62
- [ Fact ]
63
- public void Process_MergesTagBuilderFromGenerateValidationMessage ( )
104
+ [ Theory ]
105
+ [ InlineData ( "Content of validation message" , "Content of validation message" ) ]
106
+ [ InlineData ( "\r \n \r \n " , "New HTML" ) ]
107
+ public async Task ProcessAsync_MergesTagBuilderFromGenerateValidationMessage (
108
+ string outputContent , string expectedOutputContent )
64
109
{
65
110
// Arrange
66
111
var validationMessageTagHelper = new ValidationMessageTagHelper
@@ -70,58 +115,57 @@ public void Process_MergesTagBuilderFromGenerateValidationMessage()
70
115
var output = new TagHelperOutput (
71
116
"span" ,
72
117
attributes : new Dictionary < string , string > ( ) ,
73
- content : "Content of validation message" ) ;
118
+ content : outputContent ) ;
74
119
var tagBuilder = new TagBuilder ( "span2" )
75
120
{
76
121
InnerHtml = "New HTML"
77
122
} ;
78
-
79
123
tagBuilder . Attributes . Add ( "data-foo" , "bar" ) ;
80
124
tagBuilder . Attributes . Add ( "data-hello" , "world" ) ;
81
125
82
126
var expectedViewContext = CreateViewContext ( ) ;
83
127
var generator = new Mock < IHtmlGenerator > ( MockBehavior . Strict ) ;
84
- generator . Setup ( mock =>
128
+ var setup = generator . Setup ( mock =>
85
129
mock . GenerateValidationMessage ( It . IsAny < ViewContext > ( ) ,
86
130
It . IsAny < string > ( ) ,
87
131
It . IsAny < string > ( ) ,
88
132
It . IsAny < string > ( ) ,
89
- It . IsAny < object > ( ) ) )
90
- . Returns ( tagBuilder ) ;
133
+ It . IsAny < object > ( ) ) ) ;
134
+ setup . Returns ( tagBuilder ) ;
91
135
92
136
SetViewContextAndGenerator ( validationMessageTagHelper , expectedViewContext , generator . Object ) ;
93
137
94
138
// Act
95
- validationMessageTagHelper . Process ( context : null , output : output ) ;
139
+ await validationMessageTagHelper . ProcessAsync ( context : null , output : output ) ;
96
140
97
141
// Assert
98
- Assert . Equal ( output . TagName , "span2 " ) ;
142
+ Assert . Equal ( output . TagName , "span " ) ;
99
143
Assert . Equal ( 2 , output . Attributes . Count ) ;
100
144
var attribute = Assert . Single ( output . Attributes , kvp => kvp . Key . Equals ( "data-foo" ) ) ;
101
145
Assert . Equal ( "bar" , attribute . Value ) ;
102
146
attribute = Assert . Single ( output . Attributes , kvp => kvp . Key . Equals ( "data-hello" ) ) ;
103
147
Assert . Equal ( "world" , attribute . Value ) ;
104
- Assert . Equal ( "New HTML" , output . Content ) ;
148
+ Assert . Equal ( expectedOutputContent , output . Content ) ;
105
149
}
106
150
107
151
[ Fact ]
108
- public void Process_DoesNothingIfNullFor ( )
152
+ public async Task ProcessAsync_DoesNothingIfNullFor ( )
109
153
{
110
154
// Arrange
111
155
var validationMessageTagHelper = new ValidationMessageTagHelper ( ) ;
112
156
var output = new TagHelperOutput (
113
157
"span" ,
114
158
attributes : new Dictionary < string , string > ( ) ,
115
159
content : "Content of validation message" ) ;
116
-
117
160
var expectedViewContext = CreateViewContext ( ) ;
118
161
var generator = new Mock < IHtmlGenerator > ( MockBehavior . Strict ) ;
119
162
120
163
SetViewContextAndGenerator ( validationMessageTagHelper , expectedViewContext , generator . Object ) ;
121
164
122
- // Act & Assert
123
- validationMessageTagHelper . Process ( context : null , output : output ) ;
165
+ // Act
166
+ await validationMessageTagHelper . ProcessAsync ( context : null , output : output ) ;
124
167
168
+ // Assert
125
169
Assert . Equal ( "span" , output . TagName ) ;
126
170
Assert . Empty ( output . Attributes ) ;
127
171
Assert . Equal ( "Content of validation message" , output . Content ) ;
0 commit comments