1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq . Expressions ;
4+ using System . Web . Mvc ;
5+ using NUnit . Framework ;
6+ using TestStack . FluentMVCTesting . Tests . TestControllers ;
7+
8+ namespace TestStack . FluentMVCTesting . Tests
9+ {
10+ partial class ControllerResultTestShould
11+ {
12+ // Different ways that action redirects can be asserted along with the expected method name and the correct controller action call for that assertion
13+ private static readonly List < RedirectToActionTestMetadata > ActionRedirects = new List < RedirectToActionTestMetadata >
14+ {
15+ ActionRedirect ( "ActionWithNoParameters" ,
16+ t => t . ShouldRedirectTo ( c => c . ActionWithNoParameters ) ,
17+ c => c . RedirectToActionWithNoParameters ( )
18+ ) ,
19+ ActionRedirect ( "ActionWithOneParameter" ,
20+ t => t . ShouldRedirectTo ( c => c . ActionWithOneParameter ) ,
21+ c => c . RedirectToActionWithOneParameter ( )
22+ ) ,
23+ ActionRedirect ( "ActionWithOneParameter" ,
24+ t => t . ShouldRedirectTo < int > ( c => c . ActionWithOneParameter ) ,
25+ c => c . RedirectToActionWithOneParameter ( )
26+ ) ,
27+ ActionRedirect ( "ActionWithTwoParameters" ,
28+ t => t . ShouldRedirectTo < int , int > ( c => c . ActionWithTwoParameters ) ,
29+ c => c . RedirectToActionWithTwoParameters ( )
30+ ) ,
31+ ActionRedirect ( "ActionWithThreeParameters" ,
32+ t => t . ShouldRedirectTo < int , int , int > ( c => c . ActionWithThreeParameters ) ,
33+ c => c . RedirectToActionWithThreeParameters ( )
34+ ) ,
35+ ActionRedirect ( "ActionWithNoParameters" ,
36+ t => t . ShouldRedirectTo ( c => c . ActionWithNoParameters ( ) ) ,
37+ c => c . RedirectToActionWithNoParameters ( )
38+ ) ,
39+ ActionRedirect ( "ActionWithOneParameter" ,
40+ t => t . ShouldRedirectTo ( c => c . ActionWithOneParameter ( 0 ) ) ,
41+ c => c . RedirectToActionWithOneParameter ( )
42+ ) ,
43+ ActionRedirect ( "ActionWithTwoParameters" ,
44+ t => t . ShouldRedirectTo ( c => c . ActionWithTwoParameters ( 0 , 0 ) ) ,
45+ c => c . RedirectToActionWithTwoParameters ( )
46+ ) ,
47+ ActionRedirect ( "ActionWithThreeParameters" ,
48+ t => t . ShouldRedirectTo ( c => c . ActionWithThreeParameters ( 0 , 0 , 0 ) ) ,
49+ c => c . RedirectToActionWithThreeParameters ( )
50+ ) ,
51+ ActionRedirect ( "ActionWithMoreThanThreeParameters" ,
52+ t => t . ShouldRedirectTo ( c => c . ActionWithMoreThanThreeParameters ( 0 , 0 , 0 , 0 ) ) ,
53+ c => c . RedirectToActionWithMoreThanThreeParameters ( )
54+ ) ,
55+ ActionRedirect ( "ActionWithMoreThanThreeParameters" ,
56+ t => t . ShouldRedirectTo ( typeof ( ControllerResultTestController ) . GetMethod ( "ActionWithMoreThanThreeParameters" ) ) ,
57+ c => c . RedirectToActionWithMoreThanThreeParameters ( )
58+ ) ,
59+ } ;
60+
61+ // Different ways that redirects to another controller can be asserted
62+ private static readonly List < TestAction > OtherControllerRedirects = new List < TestAction >
63+ {
64+ c => c . ShouldRedirectTo < SomeOtherController > ( typeof ( SomeOtherController ) . GetMethod ( "SomeAction" ) ) ,
65+ c => c . ShouldRedirectTo < SomeOtherController > ( c2 => c2 . SomeAction ( ) ) ,
66+ } ;
67+
68+ public class RedirectToActionTestMetadata : Tuple < string , TestAction , Expression < Func < ControllerResultTestController , ActionResult > > >
69+ {
70+ public RedirectToActionTestMetadata ( string expectedMethodName , TestAction testCall , Expression < Func < ControllerResultTestController , ActionResult > > validControllerActionCall )
71+ : base ( expectedMethodName , testCall , validControllerActionCall ) { }
72+ }
73+
74+ public delegate void TestAction ( ControllerResultTest < ControllerResultTestController > testClass ) ;
75+
76+ private static Tuple < string , TestAction > ReturnType < T > ( TestAction a )
77+ {
78+ return new Tuple < string , TestAction > ( typeof ( T ) . Name , a ) ;
79+ }
80+
81+ private static RedirectToActionTestMetadata ActionRedirect ( string s , TestAction a , Expression < Func < ControllerResultTestController , ActionResult > > c )
82+ {
83+ return new RedirectToActionTestMetadata ( s , a , c ) ;
84+ }
85+
86+ [ Test ]
87+ public void Check_for_redirect_to_url ( )
88+ {
89+ _controller . WithCallTo ( c => c . RedirectToUrl ( ) ) . ShouldRedirectTo ( ControllerResultTestController . RedirectUrl ) ;
90+ }
91+
92+ [ Test ]
93+ public void Check_for_redirect_to_invalid_url ( )
94+ {
95+ const string url = "http://validurl/" ;
96+ var exception = Assert . Throws < ActionResultAssertionException > ( ( ) =>
97+ _controller . WithCallTo ( c => c . RedirectToUrl ( ) ) . ShouldRedirectTo ( url )
98+ ) ;
99+ Assert . That ( exception . Message , Is . EqualTo ( string . Format ( "Expected redirect to URL '{0}', but instead was given a redirect to URL '{1}'." , url , ControllerResultTestController . RedirectUrl ) ) ) ;
100+ }
101+
102+ [ Test ]
103+ public void Check_for_redirect_to_route_name ( )
104+ {
105+ _controller . WithCallTo ( c => c . RedirectToRouteName ( ) ) . ShouldRedirectToRoute ( ControllerResultTestController . RouteName ) ;
106+ }
107+
108+ [ Test ]
109+ public void Check_for_redirect_to_invalid_route_name ( )
110+ {
111+ const string routeName = "ValidRoute" ;
112+ var exception = Assert . Throws < ActionResultAssertionException > ( ( ) =>
113+ _controller . WithCallTo ( c => c . RedirectToRouteName ( ) ) . ShouldRedirectToRoute ( routeName )
114+ ) ;
115+ Assert . That ( exception . Message , Is . EqualTo ( string . Format ( "Expected redirect to route '{0}', but instead was given a redirect to route '{1}'." , routeName , ControllerResultTestController . RouteName ) ) ) ;
116+ }
117+
118+ [ Test ]
119+ [ TestCaseSource ( "ActionRedirects" ) ]
120+ public void Check_for_redirect_to_action ( RedirectToActionTestMetadata test )
121+ {
122+ test . Item2 ( _controller . WithCallTo ( test . Item3 ) ) ;
123+ }
124+
125+ [ Test ]
126+ [ TestCaseSource ( "ActionRedirects" ) ]
127+ public void Check_for_redirect_to_incorrect_controller ( RedirectToActionTestMetadata test )
128+ {
129+ var exception = Assert . Throws < ActionResultAssertionException > ( ( ) =>
130+ test . Item2 ( _controller . WithCallTo ( c => c . RedirectToAnotherController ( ) ) )
131+ ) ;
132+ Assert . That ( exception . Message , Is . EqualTo ( "Expected redirect to controller 'ControllerResultTest', but instead was given a redirect to controller 'SomeOther'." ) ) ;
133+ }
134+
135+ [ Test ]
136+ [ TestCaseSource ( "ActionRedirects" ) ]
137+ public void Check_for_redirect_to_incorrect_action ( RedirectToActionTestMetadata test )
138+ {
139+ var exception = Assert . Throws < ActionResultAssertionException > ( ( ) =>
140+ test . Item2 ( _controller . WithCallTo ( c => c . RedirectToRandomResult ( ) ) )
141+ ) ;
142+ Assert . That ( exception . Message , Is . EqualTo ( string . Format ( "Expected redirect to action '{0}', but instead was given a redirect to action 'RandomResult'." , test . Item1 ) ) ) ;
143+ }
144+
145+ // todo: Test the route values expectations
146+
147+ [ Test ]
148+ [ TestCaseSource ( "ActionRedirects" ) ]
149+ public void Check_for_redirect_to_empty_action ( RedirectToActionTestMetadata test )
150+ {
151+ var exception = Assert . Throws < ActionResultAssertionException > ( ( ) =>
152+ test . Item2 ( _controller . WithCallTo ( c => c . RedirectToRouteName ( ) ) )
153+ ) ;
154+ Assert . That ( exception . Message , Is . EqualTo ( string . Format ( "Expected redirect to action '{0}', but instead was given a redirect without an action." , test . Item1 ) ) ) ;
155+ }
156+
157+ [ Test ]
158+ [ TestCaseSource ( "OtherControllerRedirects" ) ]
159+ public void Check_for_redirect_to_another_controller ( TestAction action )
160+ {
161+ action ( _controller . WithCallTo ( c => c . RedirectToAnotherController ( ) ) ) ;
162+ }
163+
164+ [ Test ]
165+ public void Check_for_redirect_to_incorrect_other_controller ( )
166+ {
167+ var exception = Assert . Throws < ActionResultAssertionException > ( ( ) =>
168+ _controller . WithCallTo ( c => c . RedirectToAnotherController ( ) ) . ShouldRedirectTo < YetAnotherController > ( c => c . SomeAction ( ) )
169+ ) ;
170+ Assert . That ( exception . Message , Is . EqualTo ( "Expected redirect to controller 'YetAnother', but instead was given a redirect to controller 'SomeOther'." ) ) ;
171+ }
172+
173+ [ Test ]
174+ public void Check_for_redirect_to_incorrect_action_in_another_controller ( )
175+ {
176+ var exception = Assert . Throws < ActionResultAssertionException > ( ( ) =>
177+ _controller . WithCallTo ( c => c . RedirectToAnotherController ( ) ) . ShouldRedirectTo < SomeOtherController > ( c => c . SomeOtherAction ( ) )
178+ ) ;
179+ Assert . That ( exception . Message , Is . EqualTo ( "Expected redirect to action 'SomeOtherAction', but instead was given a redirect to action 'SomeAction'." ) ) ;
180+ }
181+
182+ [ Test ]
183+ public void Check_for_redirect_to_action_with_non_specified_controller ( )
184+ {
185+ var exception = Assert . Throws < ActionResultAssertionException > ( ( ) =>
186+ _controller . WithCallTo ( c => c . RedirectToAnotherActionNoController ( ) ) . ShouldRedirectTo < SomeOtherController > ( c => c . SomeOtherAction ( ) )
187+ ) ;
188+ Assert . That ( exception . Message , Is . EqualTo ( "Expected redirect to action 'SomeOtherAction' in 'SomeOther' controller, but instead was given redirect to action 'SomeAction' within the same controller." ) ) ;
189+ }
190+ }
191+ }
0 commit comments