1
- namespace MyTested . AspNetCore . Mvc . Builders . ExceptionErrors
1
+ namespace MyTested . AspNetCore . Mvc . Builders . CaughtExceptions
2
2
{
3
3
using System ;
4
4
using Base ;
5
- using Contracts . ExceptionErrors ;
5
+ using Contracts . CaughtExceptions ;
6
6
using Exceptions ;
7
7
using Internal . TestContexts ;
8
8
using Utilities ;
9
- using Utilities . Extensions ;
10
9
11
10
/// <summary>
12
11
/// Used for testing expected exceptions.
13
12
/// </summary>
14
- public class ExceptionTestBuilder : BaseTestBuilderWithInvokedAction , IAndExceptionTestBuilder
13
+ public class ExceptionTestBuilder : BaseTestBuilderWithComponent , IAndExceptionTestBuilder
15
14
{
16
15
/// <summary>
17
16
/// Initializes a new instance of the <see cref="ExceptionTestBuilder"/> class.
18
17
/// </summary>
19
- /// <param name="testContext"><see cref="ControllerTestContext "/> containing data about the currently executed assertion chain.</param>
20
- public ExceptionTestBuilder ( ControllerTestContext testContext )
18
+ /// <param name="testContext"><see cref="ComponentTestContext "/> containing data about the currently executed assertion chain.</param>
19
+ public ExceptionTestBuilder ( ComponentTestContext testContext )
21
20
: base ( testContext )
22
21
{
23
22
}
@@ -26,15 +25,14 @@ public ExceptionTestBuilder(ControllerTestContext testContext)
26
25
public IAndExceptionTestBuilder OfType < TException > ( )
27
26
{
28
27
var expectedExceptionType = typeof ( TException ) ;
29
- var actualExceptionType = this . CaughtException . GetType ( ) ;
28
+ var actualExceptionType = this . TestContext . CaughtException . GetType ( ) ;
30
29
if ( Reflection . AreDifferentTypes ( expectedExceptionType , actualExceptionType ) )
31
30
{
32
31
throw new InvalidExceptionAssertionException ( string . Format (
33
- "When calling {0} action in {1} expected {2}, but instead received {3}." ,
34
- this . ActionName ,
35
- this . Controller . GetName ( ) ,
32
+ "{0} {1}, but instead received {2}." ,
33
+ this . TestContext . ExceptionMessagePrefix ,
36
34
expectedExceptionType . ToFriendlyTypeName ( ) ,
37
- this . CaughtException . GetName ( ) ) ) ;
35
+ actualExceptionType . ToFriendlyTypeName ( ) ) ) ;
38
36
}
39
37
40
38
return this ;
@@ -51,13 +49,12 @@ public IExceptionMessageTestBuilder WithMessage()
51
49
/// <inheritdoc />
52
50
public IAndExceptionTestBuilder WithMessage ( string message )
53
51
{
54
- var actualExceptionMessage = this . CaughtException . Message ;
52
+ var actualExceptionMessage = this . TestContext . CaughtException . Message ;
55
53
if ( actualExceptionMessage != message )
56
54
{
57
55
throw new InvalidExceptionAssertionException ( string . Format (
58
- "When calling {0} action in {1} expected exception with message '{2}', but instead received '{3}'." ,
59
- this . ActionName ,
60
- this . Controller . GetName ( ) ,
56
+ "{0} exception with message '{1}', but instead received '{2}'." ,
57
+ this . TestContext . ExceptionMessagePrefix ,
61
58
message ,
62
59
actualExceptionMessage ) ) ;
63
60
}
@@ -68,20 +65,19 @@ public IAndExceptionTestBuilder WithMessage(string message)
68
65
/// <inheritdoc />
69
66
public IAndExceptionTestBuilder WithMessage ( Action < string > assertions )
70
67
{
71
- assertions ( this . CaughtException . Message ) ;
68
+ assertions ( this . TestContext . CaughtException . Message ) ;
72
69
return this ;
73
70
}
74
71
75
72
/// <inheritdoc />
76
73
public IAndExceptionTestBuilder WithMessage ( Func < string , bool > predicate )
77
74
{
78
- var actualExceptionMessage = this . CaughtException . Message ;
75
+ var actualExceptionMessage = this . TestContext . CaughtException . Message ;
79
76
if ( ! predicate ( actualExceptionMessage ) )
80
77
{
81
78
throw new InvalidExceptionAssertionException ( string . Format (
82
- "When calling {0} action in {1} expected exception message ('{2}') to pass the given predicate, but it failed." ,
83
- this . ActionName ,
84
- this . Controller . GetName ( ) ,
79
+ "{0} exception message ('{1}') to pass the given predicate, but it failed." ,
80
+ this . TestContext . ExceptionMessagePrefix ,
85
81
actualExceptionMessage ) ) ;
86
82
}
87
83
0 commit comments