55
66namespace FluentAssertions . AspNetCore . Mvc . Tests
77{
8-
8+
99 public class AcceptedAtActionResultAssertions_Tests
1010 {
11+ public const string Reason = FailureMessageHelper . Reason ;
12+ public readonly static object [ ] ReasonArgs = FailureMessageHelper . ReasonArgs ;
1113 private const string TestValue = "testValue" ;
1214
1315 [ Fact ]
1416 public void WithActionName_GivenExpectedActionName_ShouldPass ( )
1517 {
1618 var expectedActionName = "expectedAction" ;
1719 var result = new AcceptedAtActionResult ( expectedActionName , string . Empty , null , null ) ;
20+
1821 result . Should ( ) . BeAcceptedAtActionResult ( ) . WithActionName ( expectedActionName ) ;
1922 }
2023
2124 [ Fact ]
2225 public void WithActionName_GivenUnexpectedActionName_ShouldFail ( )
2326 {
2427 var result = new AcceptedAtActionResult ( "someOtherAction" , string . Empty , null , null ) ;
25- Action a = ( ) => result . Should ( ) . BeAcceptedAtActionResult ( ) . WithActionName ( "expectedAction" ) ;
26- a . Should ( ) . Throw < Exception > ( ) . WithMessage ( "Expected AcceptedAtActionResult.ActionName to be \" expectedAction\" but was \" someOtherAction\" " ) ;
28+ var failureMessage = FailureMessageHelper . ExpectedContextToBeXButY (
29+ "AcceptedAtActionResult.ActionName" , "expectedAction" , "someOtherAction" ) ;
30+
31+ Action a = ( ) => result . Should ( ) . BeAcceptedAtActionResult ( ) . WithActionName ( "expectedAction" , Reason , ReasonArgs ) ;
32+
33+ a . Should ( ) . Throw < Exception > ( ) . WithMessage ( failureMessage ) ;
2734 }
2835
2936 [ Fact ]
3037 public void WithControllerName_GivenExpectedControllerName_ShouldPass ( )
3138 {
3239 var expectedControllerName = "expectedController" ;
3340 var result = new AcceptedAtActionResult ( string . Empty , expectedControllerName , null , null ) ;
41+
3442 result . Should ( ) . BeAcceptedAtActionResult ( ) . WithControllerName ( expectedControllerName ) ;
3543 }
3644
3745 [ Fact ]
3846 public void WithControllerName_GivenUnexpectedControllerName_ShouldFail ( )
3947 {
4048 var result = new AcceptedAtActionResult ( string . Empty , "someOtherController" , null , null ) ;
41- Action a = ( ) => result . Should ( ) . BeAcceptedAtActionResult ( ) . WithControllerName ( "expectedController" ) ;
42- a . Should ( ) . Throw < Exception > ( ) . WithMessage ( "Expected AcceptedAtActionResult.ControllerName to be \" expectedController\" but was \" someOtherController\" " ) ;
49+ var failureMessage = FailureMessageHelper . ExpectedContextToBeXButY (
50+ "AcceptedAtActionResult.ControllerName" , "expectedController" , "someOtherController" ) ;
51+
52+ Action a = ( ) => result . Should ( ) . BeAcceptedAtActionResult ( ) . WithControllerName ( "expectedController" , Reason , ReasonArgs ) ;
53+
54+ a . Should ( ) . Throw < Exception > ( ) . WithMessage ( failureMessage ) ;
4355 }
4456
4557 [ Fact ]
4658 public void WithRouteValue_GivenKeyDoesntExist_ShouldFail ( )
4759 {
4860 var expectedKey = "expectedKey" ;
49- var failureMessage = FailureMessageHelper . Format ( FailureMessages . AcceptedAtActionResult_RouteValues_ContainsKey , expectedKey ) ;
50-
51- var routeValues = new { myKey = "MyValue" } ;
61+ var failureMessage = FailureMessageHelper . ExpectedContextContainValueAtKeyButKeyNotFound
62+ ( "AcceptedAtActionResult.RouteValues" , "MyValue" , expectedKey ) ;
63+ var routeValues = new { myKey = "MyValue" } ;
5264 var result = new AcceptedAtActionResult ( string . Empty , string . Empty , routeValues , null ) ;
5365
54- Action a = ( ) => result . Should ( ) . BeAcceptedAtActionResult ( ) . WithRouteValue ( expectedKey , "" ) ;
66+ Action a = ( ) => result . Should ( ) . BeAcceptedAtActionResult ( ) . WithRouteValue ( expectedKey , "MyValue" , Reason , ReasonArgs ) ;
67+
5568 a . Should ( ) . Throw < Exception > ( ) . WithMessage ( failureMessage ) ;
5669 }
5770
@@ -60,9 +73,9 @@ public void WithRouteValue_GivenExpectedKeyValuePair_ShouldPass()
6073 {
6174 var expectedKey = "expectedKey" ;
6275 var expectedValue = "expectedValue" ;
63- var routeValues = new { expectedKey = expectedValue } ;
64-
76+ var routeValues = new { expectedKey = expectedValue } ;
6577 var result = new AcceptedAtActionResult ( string . Empty , string . Empty , routeValues , null ) ;
78+
6679 result . Should ( ) . BeAcceptedAtActionResult ( ) . WithRouteValue ( expectedKey , expectedValue ) ;
6780 }
6881
@@ -72,42 +85,43 @@ public void HaveValue_GivenUnexpectedKeyValuePair_ShouldFail()
7285 var expectedKey = "expectedKey" ;
7386 var expectedValue = "expectedValue" ;
7487 var routeValues = new { expectedKey = "someOtherValue" } ;
75- var failureMessage = FailureMessageHelper . Format ( FailureMessages . AcceptedAtActionResult_RouteValues_HaveValue , expectedKey , expectedValue , "someOtherValue" ) ;
76-
88+ var failureMessage = FailureMessageHelper . ExpectedAtKeyValueXButFoundY (
89+ "AcceptedAtActionResult.RouteValues" , expectedKey , expectedValue , "someOtherValue" ) ;
7790 var result = new AcceptedAtActionResult ( string . Empty , string . Empty , routeValues , null ) ;
78- Action a = ( ) => result . Should ( ) . BeAcceptedAtActionResult ( ) . WithRouteValue ( expectedKey , expectedValue ) ;
91+
92+ Action a = ( ) => result . Should ( ) . BeAcceptedAtActionResult ( ) . WithRouteValue ( expectedKey , expectedValue , Reason , ReasonArgs ) ;
93+
7994 a . Should ( ) . Throw < Exception > ( ) . WithMessage ( failureMessage ) ;
8095 }
8196
8297 [ Fact ]
8398 public void ValueAs_GivenExpectedValue_ShouldPass ( )
8499 {
85100 var result = new TestController ( ) . AcceptedAtAction ( string . Empty , string . Empty , null , TestValue ) ;
86- result . Should ( ) . BeAcceptedAtActionResult ( ) . ValueAs < string > ( ) . Should ( ) . Be ( TestValue ) ;
87- }
88-
89- [ Fact ]
90- public void ValueAs_GivenUnexpectedValue_ShouldFail ( )
91- {
92- var result = new TestController ( ) . AcceptedAtAction ( string . Empty , string . Empty , null , TestValue ) ;
93- Action a = ( ) => result . Should ( ) . BeAcceptedAtActionResult ( ) . ValueAs < string > ( ) . Should ( ) . Be ( "xyx" ) ;
94- a . Should ( ) . Throw < Exception > ( ) ;
101+ result . Should ( ) . BeAcceptedAtActionResult ( ) . ValueAs < string > ( ) . Should ( ) . BeSameAs ( TestValue ) ;
95102 }
96103
97104 [ Fact ]
98105 public void ValueAs_GivenWrongType_ShouldFail ( )
99106 {
100107 var result = new TestController ( ) . AcceptedAtAction ( string . Empty , string . Empty , null , TestValue ) ;
101- Action a = ( ) => result . Should ( ) . BeAcceptedAtActionResult ( ) . ValueAs < int > ( ) . Should ( ) . Be ( 2 ) ;
108+ var failureMessage = FailureMessageHelper . ExpectedContextTypeXButFoundY (
109+ "AcceptedAtActionResult.Value" , typeof ( int ) , typeof ( string ) ) ;
110+
111+ Action a = ( ) => result . Should ( ) . BeAcceptedAtActionResult ( ) . ValueAs < int > ( ) ;
112+
102113 a . Should ( ) . Throw < Exception > ( ) ;
103114 }
104115
105116 [ Fact ]
106117 public void ValueAs_Null_ShouldFail ( )
107118 {
108119 ActionResult result = new AcceptedAtActionResult ( string . Empty , string . Empty , null , null ) ;
109- var failureMessage = FailureMessageHelper . Format ( FailureMessages . CommonNullWasSuppliedFailMessage , "AcceptedAtActionResult.Value" , typeof ( object ) . Name ) ;
120+ var failureMessage = FailureMessageHelper . ExpectedContextTypeXButFoundNull (
121+ "AcceptedAtActionResult.Value" , typeof ( object ) ) ;
122+
110123 Action a = ( ) => result . Should ( ) . BeAcceptedAtActionResult ( ) . ValueAs < object > ( ) ;
124+
111125 a . Should ( ) . Throw < Exception > ( ) . WithMessage ( failureMessage ) ;
112126 }
113127 }
0 commit comments