File tree Expand file tree Collapse file tree 5 files changed +208
-2
lines changed
test/MyTested.AspNetCore.Mvc.ViewData.Test
BuildersTests/ActionsTests/ShouldHaveTests Expand file tree Collapse file tree 5 files changed +208
-2
lines changed Original file line number Diff line number Diff line change @@ -131,5 +131,47 @@ public void ViewBagWithBuilderShouldWorkCorrectly()
131
131
. ShouldReturn ( )
132
132
. Ok ( ) ;
133
133
}
134
+
135
+ [ Fact ]
136
+ public void ViewBagWithBuilderShouldContainMultipleEntries ( )
137
+ {
138
+ MyController < MvcController >
139
+ . Instance ( )
140
+ . Calling ( c => c . AddViewBagAction ( ) )
141
+ . ShouldHave ( )
142
+ . ViewBag ( viewBag => viewBag
143
+ . ContainingEntries ( new { Test = "BagValue" , Another = "AnotherValue" } ) )
144
+ . AndAlso ( )
145
+ . ShouldReturn ( )
146
+ . Ok ( ) ;
147
+ }
148
+
149
+ [ Fact ]
150
+ public void ViewBagWithBuilderShouldContainEntryWithKey ( )
151
+ {
152
+ MyController < MvcController >
153
+ . Instance ( )
154
+ . Calling ( c => c . AddViewBagAction ( ) )
155
+ . ShouldHave ( )
156
+ . ViewBag ( viewBag => viewBag
157
+ . ContainingEntryWithKey ( "Another" ) )
158
+ . AndAlso ( )
159
+ . ShouldReturn ( )
160
+ . Ok ( ) ;
161
+ }
162
+
163
+ [ Fact ]
164
+ public void ViewBagWithBuilderShouldContainEntryOfTypeString ( )
165
+ {
166
+ MyController < MvcController >
167
+ . Instance ( )
168
+ . Calling ( c => c . AddViewBagAction ( ) )
169
+ . ShouldHave ( )
170
+ . ViewBag ( viewBag => viewBag
171
+ . ContainingEntryOfType < string > ( ) )
172
+ . AndAlso ( )
173
+ . ShouldReturn ( )
174
+ . Ok ( ) ;
175
+ }
134
176
}
135
177
}
Original file line number Diff line number Diff line change @@ -131,5 +131,47 @@ public void ViewDataWithBuilderShouldWorkCorrectly()
131
131
. ShouldReturn ( )
132
132
. Ok ( ) ;
133
133
}
134
+
135
+ [ Fact ]
136
+ public void ViewDataWithBuilderShouldContainMultipleEntries ( )
137
+ {
138
+ MyController < MvcController >
139
+ . Instance ( )
140
+ . Calling ( c => c . AddViewDataAction ( ) )
141
+ . ShouldHave ( )
142
+ . ViewData ( viewData => viewData
143
+ . ContainingEntries ( new { Test = "DataValue" , Another = "AnotherValue" } ) )
144
+ . AndAlso ( )
145
+ . ShouldReturn ( )
146
+ . Ok ( ) ;
147
+ }
148
+
149
+ [ Fact ]
150
+ public void ViewDataWithBuilderShouldContainEntryWithKey ( )
151
+ {
152
+ MyController < MvcController >
153
+ . Instance ( )
154
+ . Calling ( c => c . AddViewDataAction ( ) )
155
+ . ShouldHave ( )
156
+ . ViewData ( viewData => viewData
157
+ . ContainingEntryWithKey ( "Another" ) )
158
+ . AndAlso ( )
159
+ . ShouldReturn ( )
160
+ . Ok ( ) ;
161
+ }
162
+
163
+ [ Fact ]
164
+ public void ViewDataWithBuilderShouldContainEntryOfTypeString ( )
165
+ {
166
+ MyController < MvcController >
167
+ . Instance ( )
168
+ . Calling ( c => c . AddViewDataAction ( ) )
169
+ . ShouldHave ( )
170
+ . ViewData ( viewData => viewData
171
+ . ContainingEntryOfType < string > ( ) )
172
+ . AndAlso ( )
173
+ . ShouldReturn ( )
174
+ . Ok ( ) ;
175
+ }
134
176
}
135
177
}
Original file line number Diff line number Diff line change
1
+ namespace MyTested . AspNetCore . Mvc . Test . InternalTests
2
+ {
3
+ using Microsoft . AspNetCore . Http ;
4
+ using Microsoft . Extensions . DependencyInjection . Extensions ;
5
+ using Setups ;
6
+ using Setups . Controllers ;
7
+ using Setups . ViewComponents ;
8
+ using System ;
9
+ using Xunit ;
10
+
11
+ public class ComponentTestContextViewDataExtensionsTests
12
+ {
13
+ [ Fact ]
14
+ public void GetViewDataShouldThrowInvalidOperationExceptionWithPocoViewComponent ( )
15
+ {
16
+ Test . AssertException < InvalidOperationException > (
17
+ ( ) =>
18
+ {
19
+ MyViewComponent < PocoViewComponent >
20
+ . Instance ( )
21
+ . InvokedWith ( c => c . Invoke ( ) )
22
+ . ShouldHave ( )
23
+ . NoViewData ( )
24
+ . AndAlso ( )
25
+ . ShouldReturn ( )
26
+ . View ( ) ;
27
+ } ,
28
+ "ViewDataDictionary could not be found on the provided PocoViewComponent. The property should be specified manually by providing component instance or using the specified helper methods." ) ;
29
+ }
30
+
31
+ [ Fact ]
32
+ public void GetViewDataShouldHaveNoViewDataForPocoController ( )
33
+ {
34
+ MyApplication
35
+ . StartsFrom < DefaultStartup > ( )
36
+ . WithServices ( services =>
37
+ {
38
+ services . TryAddSingleton < IHttpContextAccessor , HttpContextAccessor > ( ) ;
39
+ } ) ;
40
+
41
+ MyController < FullPocoController >
42
+ . Instance ( )
43
+ . Calling ( c => c . OkResultAction ( ) )
44
+ . ShouldHave ( )
45
+ . NoViewData ( )
46
+ . AndAlso ( )
47
+ . ShouldReturn ( )
48
+ . Ok ( ) ;
49
+
50
+ MyApplication . StartsFrom < DefaultStartup > ( ) ;
51
+ }
52
+ }
53
+ }
Original file line number Diff line number Diff line change 4
4
using Internal . Services ;
5
5
using Microsoft . AspNetCore . Http ;
6
6
using Microsoft . AspNetCore . Mvc ;
7
- using Setups . Controllers ;
8
- using Xunit ;
9
7
using Microsoft . Extensions . DependencyInjection . Extensions ;
10
8
using Setups ;
9
+ using Setups . Controllers ;
10
+ using System ;
11
+ using Xunit ;
11
12
12
13
public class ViewDataPropertyHelperTests
13
14
{
@@ -58,5 +59,31 @@ public void GetPropertiesShouldNotThrowExceptionForPocoController()
58
59
59
60
MyApplication . StartsFrom < DefaultStartup > ( ) ;
60
61
}
62
+
63
+ [ Fact ]
64
+ public void ViewDataGetterShouldThrowNullReferenceExceptionWithNull ( )
65
+ {
66
+ MyApplication . StartsFrom < DefaultStartup > ( ) ;
67
+
68
+ var helper = ViewDataPropertyHelper . GetViewDataProperties < MvcController > ( ) ;
69
+
70
+ Assert . Throws < NullReferenceException > ( ( ) => helper . ViewDataGetter ( null ) ) ;
71
+ }
72
+
73
+ [ Fact ]
74
+ public void ViewDataGetterShouldThrowInvalidOperationExceptionForPrivatePocoController ( )
75
+ {
76
+ MyApplication . StartsFrom < DefaultStartup > ( ) ;
77
+
78
+ var helper = ViewDataPropertyHelper . GetViewDataProperties < PrivatePocoController > ( ) ;
79
+ var controller = new PrivatePocoController ( TestServiceProvider . Global ) ;
80
+
81
+ Test . AssertException < InvalidOperationException > (
82
+ ( ) =>
83
+ {
84
+ helper . ViewDataGetter ( controller ) ;
85
+ } ,
86
+ "ViewDataDictionary could not be found on the provided PrivatePocoController. The property should be specified manually by providing component instance or using the specified helper methods." ) ;
87
+ }
61
88
}
62
89
}
Original file line number Diff line number Diff line change
1
+ namespace MyTested . AspNetCore . Mvc . Test . PluginsTests
2
+ {
3
+ using Microsoft . Extensions . DependencyInjection ;
4
+ using Plugins ;
5
+ using System ;
6
+ using Xunit ;
7
+
8
+ public class ViewDataTestPluginTests
9
+ {
10
+ [ Fact ]
11
+ public void ShouldHavePriorityWithDefaultValue ( )
12
+ {
13
+ var testPlugin = new ViewDataTestPlugin ( ) ;
14
+
15
+ Assert . IsAssignableFrom < IDefaultRegistrationPlugin > ( testPlugin ) ;
16
+ Assert . NotNull ( testPlugin ) ;
17
+ Assert . Equal ( - 1000 , testPlugin . Priority ) ;
18
+ }
19
+
20
+ [ Fact ]
21
+ public void ShouldThrowArgumentNullExceptionWithInvalidServiceCollection ( )
22
+ {
23
+ var testPlugin = new ViewDataTestPlugin ( ) ;
24
+
25
+ Assert . Throws < ArgumentNullException > ( ( ) => testPlugin . DefaultServiceRegistrationDelegate ( null ) ) ;
26
+ }
27
+
28
+ [ Fact ]
29
+ public void ShouldInvokeMethodOfTypeVoidWithValidServiceCollection ( )
30
+ {
31
+ var testPlugin = new ViewDataTestPlugin ( ) ;
32
+ var serviceCollection = new ServiceCollection ( ) ;
33
+
34
+ testPlugin . DefaultServiceRegistrationDelegate ( serviceCollection ) ;
35
+
36
+ var methodReturnType = testPlugin . DefaultServiceRegistrationDelegate . Method . ReturnType . Name ;
37
+
38
+ Assert . True ( methodReturnType == "Void" ) ;
39
+ Assert . True ( serviceCollection . Count == 126 ) ;
40
+ }
41
+ }
42
+ }
You can’t perform that action at this time.
0 commit comments