@@ -14,6 +14,63 @@ public static void ValidateInvocationResultType(
1414 bool canBeAssignable = false ,
1515 bool allowDifferentGenericTypeDefinitions = false ,
1616 Type typeOfActualReturnValue = null )
17+ {
18+ var invalid = InvocationResultTypeIsInvalid (
19+ testContext ,
20+ typeOfExpectedReturnValue ,
21+ canBeAssignable ,
22+ allowDifferentGenericTypeDefinitions ,
23+ typeOfActualReturnValue ) ;
24+
25+ if ( invalid )
26+ {
27+ var typeOfResult = typeOfActualReturnValue ?? testContext . MethodResult ? . GetType ( ) ;
28+
29+ var ( expectedTypeName , actualTypeName ) = ( typeOfExpectedReturnValue , typeOfResult ) . GetTypeComparisonNames ( ) ;
30+
31+ ThrowNewInvocationResultAssertionException (
32+ testContext ,
33+ expectedTypeName ,
34+ actualTypeName ) ;
35+ }
36+ }
37+
38+ public static void ValidateInvocationResultType < TExpectedType > (
39+ ComponentTestContext testContext ,
40+ bool canBeAssignable = false ,
41+ bool allowDifferentGenericTypeDefinitions = false )
42+ => ValidateInvocationResultType ( testContext , typeof ( TExpectedType ) , canBeAssignable , allowDifferentGenericTypeDefinitions ) ;
43+
44+ public static void ValidateInvocationResult < TResult > ( ComponentTestContext testContext , TResult model , bool canBeAssignable = false )
45+ {
46+ if ( ! Reflection . IsAnonymousType ( typeof ( TResult ) ) )
47+ {
48+ ValidateInvocationResultType < TResult > ( testContext , canBeAssignable ) ;
49+ }
50+
51+ if ( Reflection . AreNotDeeplyEqual ( model , testContext . MethodResult , out var result ) )
52+ {
53+ throw ResponseModelAssertionException . From ( testContext . ExceptionMessagePrefix , result ) ;
54+ }
55+
56+ testContext . Model = model ;
57+ }
58+
59+ public static TResult GetInvocationResult < TResult > (
60+ ComponentTestContext testContext ,
61+ bool canBeAssignable = false )
62+ where TResult : class
63+ {
64+ ValidateInvocationResultType < TResult > ( testContext , canBeAssignable ) ;
65+ return testContext . MethodResult as TResult ;
66+ }
67+
68+ private static bool InvocationResultTypeIsInvalid (
69+ ComponentTestContext testContext ,
70+ Type typeOfExpectedReturnValue ,
71+ bool canBeAssignable = false ,
72+ bool allowDifferentGenericTypeDefinitions = false ,
73+ Type typeOfActualReturnValue = null )
1774 {
1875 InvocationValidator . CheckForException ( testContext . CaughtException , testContext . ExceptionMessagePrefix ) ;
1976
@@ -64,45 +121,7 @@ public static void ValidateInvocationResultType(
64121 invalid = ! Reflection . AreAssignableByGeneric ( typeOfExpectedReturnValue , typeOfResult ) ;
65122 }
66123
67- if ( invalid )
68- {
69- var ( expectedTypeName , actualTypeName ) = ( typeOfExpectedReturnValue , typeOfResult ) . GetTypeComparisonNames ( ) ;
70-
71- ThrowNewInvocationResultAssertionException (
72- testContext ,
73- expectedTypeName ,
74- actualTypeName ) ;
75- }
76- }
77-
78- public static void ValidateInvocationResultType < TExpectedType > (
79- ComponentTestContext testContext ,
80- bool canBeAssignable = false ,
81- bool allowDifferentGenericTypeDefinitions = false )
82- => ValidateInvocationResultType ( testContext , typeof ( TExpectedType ) , canBeAssignable , allowDifferentGenericTypeDefinitions ) ;
83-
84- public static void ValidateInvocationResult < TResult > ( ComponentTestContext testContext , TResult model , bool canBeAssignable = false )
85- {
86- if ( ! Reflection . IsAnonymousType ( typeof ( TResult ) ) )
87- {
88- ValidateInvocationResultType < TResult > ( testContext , canBeAssignable ) ;
89- }
90-
91- if ( Reflection . AreNotDeeplyEqual ( model , testContext . MethodResult , out var result ) )
92- {
93- throw ResponseModelAssertionException . From ( testContext . ExceptionMessagePrefix , result ) ;
94- }
95-
96- testContext . Model = model ;
97- }
98-
99- public static TResult GetInvocationResult < TResult > (
100- ComponentTestContext testContext ,
101- bool canBeAssignable = false )
102- where TResult : class
103- {
104- ValidateInvocationResultType < TResult > ( testContext , canBeAssignable ) ;
105- return testContext . MethodResult as TResult ;
124+ return invalid ;
106125 }
107126
108127 private static void ThrowNewInvocationResultAssertionException (
0 commit comments