Skip to content

Commit b410482

Browse files
committed
Extracted exception test builder so that view components can use them (#66)
1 parent 1048faa commit b410482

File tree

56 files changed

+512
-221
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+512
-221
lines changed

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Attributes/BaseAttributesTestBuilder.cs

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
using System.Linq;
66
using Base;
77
using Internal.TestContexts;
8-
using Microsoft.AspNetCore.Authorization;
9-
using Microsoft.AspNetCore.Mvc;
108
using Utilities;
11-
using Utilities.Extensions;
129

1310
/// <summary>
1411
/// Base class for all attribute test builders.
@@ -56,76 +53,6 @@ protected void ContainingAttributeOfType<TAttribute>(Action<string, string> fail
5653
});
5754
}
5855

59-
/// <summary>
60-
/// Tests whether the action attributes contain RouteAttribute.
61-
/// </summary>
62-
/// <param name="template">Expected overridden route template of the action.</param>
63-
/// <param name="failedValidationAction">Action to execute, if the validation fails.</param>
64-
/// <param name="withName">Optional expected route name.</param>
65-
/// <param name="withOrder">Optional expected route order.</param>
66-
protected void ChangingRouteTo(
67-
string template,
68-
Action<string, string> failedValidationAction,
69-
string withName = null,
70-
int? withOrder = null)
71-
{
72-
this.ContainingAttributeOfType<RouteAttribute>(failedValidationAction);
73-
this.Validations.Add(attrs =>
74-
{
75-
var routeAttribute = this.TryGetAttributeOfType<RouteAttribute>(attrs);
76-
var actualTemplate = routeAttribute.Template;
77-
if (!string.Equals(template, actualTemplate, StringComparison.OrdinalIgnoreCase))
78-
{
79-
failedValidationAction(
80-
$"{routeAttribute.GetName()} with '{template}' template",
81-
$"in fact found '{actualTemplate}'");
82-
}
83-
84-
var actualName = routeAttribute.Name;
85-
if (!string.IsNullOrEmpty(withName) && withName != actualName)
86-
{
87-
failedValidationAction(
88-
$"{routeAttribute.GetName()} with '{withName}' name",
89-
$"in fact found '{actualName}'");
90-
}
91-
92-
var actualOrder = routeAttribute.Order;
93-
if (withOrder.HasValue && withOrder != actualOrder)
94-
{
95-
failedValidationAction(
96-
$"{routeAttribute.GetName()} with order of {withOrder}",
97-
$"in fact found {actualOrder}");
98-
}
99-
});
100-
}
101-
102-
/// <summary>
103-
/// Tests whether the action attributes contain <see cref="Microsoft.AspNetCore.Authorization.AuthorizeAttribute"/>.
104-
/// </summary>
105-
/// <param name="failedValidationAction">Action to execute, if the validation fails.</param>
106-
/// <param name="withAllowedRoles">Optional expected authorized roles.</param>
107-
protected void RestrictingForAuthorizedRequests(
108-
Action<string, string> failedValidationAction,
109-
string withAllowedRoles = null)
110-
{
111-
this.ContainingAttributeOfType<AuthorizeAttribute>(failedValidationAction);
112-
var testAllowedRoles = !string.IsNullOrEmpty(withAllowedRoles);
113-
if (testAllowedRoles)
114-
{
115-
this.Validations.Add(attrs =>
116-
{
117-
var authorizeAttribute = this.GetAttributeOfType<AuthorizeAttribute>(attrs);
118-
var actualRoles = authorizeAttribute.Roles;
119-
if (withAllowedRoles != actualRoles)
120-
{
121-
failedValidationAction(
122-
$"{authorizeAttribute.GetName()} with allowed '{withAllowedRoles}' roles",
123-
$"in fact found '{actualRoles}'");
124-
}
125-
});
126-
}
127-
}
128-
12956
/// <summary>
13057
/// Gets an attribute of the given type from the provided collection of objects and throws exception if such is not found.
13158
/// </summary>
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
namespace MyTested.AspNetCore.Mvc.Builders.ExceptionErrors
1+
namespace MyTested.AspNetCore.Mvc.Builders.CaughtExceptions
22
{
33
using System;
44
using System.Linq;
5-
using Contracts.ExceptionErrors;
5+
using Contracts.CaughtExceptions;
66
using Exceptions;
77
using Internal.TestContexts;
88
using Utilities;
9-
using Utilities.Extensions;
109

1110
/// <summary>
1211
/// Used for testing <see cref="AggregateException"/>.
@@ -18,8 +17,8 @@ public class AggregateExceptionTestBuilder : ExceptionTestBuilder, IAndAggregate
1817
/// <summary>
1918
/// Initializes a new instance of the <see cref="AggregateExceptionTestBuilder"/> class.
2019
/// </summary>
21-
/// <param name="testContext"><see cref="ControllerTestContext"/> containing data about the currently executed assertion chain.</param>
22-
public AggregateExceptionTestBuilder(ControllerTestContext testContext)
20+
/// <param name="testContext"><see cref="ComponentTestContext"/> containing data about the currently executed assertion chain.</param>
21+
public AggregateExceptionTestBuilder(ComponentTestContext testContext)
2322
: base(testContext)
2423
{
2524
this.aggregateException = testContext.CaughtExceptionAs<AggregateException>();
@@ -34,9 +33,9 @@ public IAndAggregateExceptionTestBuilder ContainingInnerExceptionOfType<TInnerEx
3433
if (!innerExceptionFound)
3534
{
3635
throw new InvalidExceptionAssertionException(string.Format(
37-
"When calling {0} action in {1} expected AggregateException to contain {2}, but none was found.",
38-
this.ActionName,
39-
this.Controller.GetName(),
36+
"{0} {1} to contain {2}, but none was found.",
37+
this.TestContext.ExceptionMessagePrefix,
38+
nameof(AggregateException),
4039
expectedInnerExceptionType.ToFriendlyTypeName()));
4140
}
4241

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
namespace MyTested.AspNetCore.Mvc.Builders.ExceptionErrors
1+
namespace MyTested.AspNetCore.Mvc.Builders.CaughtExceptions
22
{
33
using Base;
4-
using Contracts.ExceptionErrors;
4+
using Contracts.CaughtExceptions;
55
using Exceptions;
66
using Internal.TestContexts;
7-
using Utilities.Extensions;
87

98
/// <summary>
109
/// Used for testing <see cref="System.Exception"/> messages.
1110
/// </summary>
1211
public class ExceptionMessageTestBuilder
13-
: BaseTestBuilderWithInvokedAction, IExceptionMessageTestBuilder
12+
: BaseTestBuilderWithComponent, IExceptionMessageTestBuilder
1413
{
1514
private readonly IAndExceptionTestBuilder exceptionTestBuilder;
1615
private readonly string actualMessage;
1716

1817
/// <summary>
1918
/// Initializes a new instance of the <see cref="ExceptionMessageTestBuilder"/> class.
2019
/// </summary>
21-
/// <param name="testContext"><see cref="ControllerTestContext"/> containing data about the currently executed assertion chain.</param>
20+
/// <param name="testContext"><see cref="ComponentTestContext"/> containing data about the currently executed assertion chain.</param>
2221
/// <param name="exceptionTestBuilder">Test builder of <see cref="IAndExceptionTestBuilder"/> type.</param>
2322
public ExceptionMessageTestBuilder(
24-
ControllerTestContext testContext,
23+
ComponentTestContext testContext,
2524
IAndExceptionTestBuilder exceptionTestBuilder)
2625
: base(testContext)
2726
{
@@ -35,7 +34,7 @@ public IAndExceptionTestBuilder ThatEquals(string errorMessage)
3534
if (this.actualMessage != errorMessage)
3635
{
3736
this.ThrowNewInvalidExceptionAssertionException(
38-
"When calling {0} action in {1} expected exception message to be '{2}', but instead found '{3}'.",
37+
"{0} exception message to be '{1}', but instead found '{2}'.",
3938
errorMessage);
4039
}
4140

@@ -48,7 +47,7 @@ public IAndExceptionTestBuilder BeginningWith(string beginMessage)
4847
if (!this.actualMessage.StartsWith(beginMessage))
4948
{
5049
this.ThrowNewInvalidExceptionAssertionException(
51-
"When calling {0} action in {1} expected exception message to begin with '{2}', but instead found '{3}'.",
50+
"{0} exception message to begin with '{1}', but instead found '{2}'.",
5251
beginMessage);
5352
}
5453

@@ -61,7 +60,7 @@ public IAndExceptionTestBuilder EndingWith(string endMessage)
6160
if (!this.actualMessage.EndsWith(endMessage))
6261
{
6362
this.ThrowNewInvalidExceptionAssertionException(
64-
"When calling {0} action in {1} expected exception message to end with '{2}', but instead found '{3}'.",
63+
"{0} exception message to end with '{1}', but instead found '{2}'.",
6564
endMessage);
6665
}
6766

@@ -74,7 +73,7 @@ public IAndExceptionTestBuilder Containing(string containsMessage)
7473
if (!this.actualMessage.Contains(containsMessage))
7574
{
7675
this.ThrowNewInvalidExceptionAssertionException(
77-
"When calling {0} action in {1} expected exception message to contain '{2}', but instead found '{3}'.",
76+
"{0} exception message to contain '{1}', but instead found '{2}'.",
7877
containsMessage);
7978
}
8079

@@ -85,8 +84,7 @@ private void ThrowNewInvalidExceptionAssertionException(string messageFormat, st
8584
{
8685
throw new InvalidExceptionAssertionException(string.Format(
8786
messageFormat,
88-
this.ActionName,
89-
this.Controller.GetName(),
87+
this.TestContext.ExceptionMessagePrefix,
9088
operation,
9189
this.actualMessage));
9290
}

src/MyTested.AspNetCore.Mvc.Controllers/Builders/ExceptionErrors/ExceptionTestBuilder.cs renamed to src/MyTested.AspNetCore.Mvc.Abstractions/Builders/CaughtExceptions/ExceptionTestBuilder.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
namespace MyTested.AspNetCore.Mvc.Builders.ExceptionErrors
1+
namespace MyTested.AspNetCore.Mvc.Builders.CaughtExceptions
22
{
33
using System;
44
using Base;
5-
using Contracts.ExceptionErrors;
5+
using Contracts.CaughtExceptions;
66
using Exceptions;
77
using Internal.TestContexts;
88
using Utilities;
9-
using Utilities.Extensions;
109

1110
/// <summary>
1211
/// Used for testing expected exceptions.
1312
/// </summary>
14-
public class ExceptionTestBuilder : BaseTestBuilderWithInvokedAction, IAndExceptionTestBuilder
13+
public class ExceptionTestBuilder : BaseTestBuilderWithComponent, IAndExceptionTestBuilder
1514
{
1615
/// <summary>
1716
/// Initializes a new instance of the <see cref="ExceptionTestBuilder"/> class.
1817
/// </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)
2120
: base(testContext)
2221
{
2322
}
@@ -26,15 +25,14 @@ public ExceptionTestBuilder(ControllerTestContext testContext)
2625
public IAndExceptionTestBuilder OfType<TException>()
2726
{
2827
var expectedExceptionType = typeof(TException);
29-
var actualExceptionType = this.CaughtException.GetType();
28+
var actualExceptionType = this.TestContext.CaughtException.GetType();
3029
if (Reflection.AreDifferentTypes(expectedExceptionType, actualExceptionType))
3130
{
3231
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,
3634
expectedExceptionType.ToFriendlyTypeName(),
37-
this.CaughtException.GetName()));
35+
actualExceptionType.ToFriendlyTypeName()));
3836
}
3937

4038
return this;
@@ -51,13 +49,12 @@ public IExceptionMessageTestBuilder WithMessage()
5149
/// <inheritdoc />
5250
public IAndExceptionTestBuilder WithMessage(string message)
5351
{
54-
var actualExceptionMessage = this.CaughtException.Message;
52+
var actualExceptionMessage = this.TestContext.CaughtException.Message;
5553
if (actualExceptionMessage != message)
5654
{
5755
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,
6158
message,
6259
actualExceptionMessage));
6360
}
@@ -68,20 +65,19 @@ public IAndExceptionTestBuilder WithMessage(string message)
6865
/// <inheritdoc />
6966
public IAndExceptionTestBuilder WithMessage(Action<string> assertions)
7067
{
71-
assertions(this.CaughtException.Message);
68+
assertions(this.TestContext.CaughtException.Message);
7269
return this;
7370
}
7471

7572
/// <inheritdoc />
7673
public IAndExceptionTestBuilder WithMessage(Func<string, bool> predicate)
7774
{
78-
var actualExceptionMessage = this.CaughtException.Message;
75+
var actualExceptionMessage = this.TestContext.CaughtException.Message;
7976
if (!predicate(actualExceptionMessage))
8077
{
8178
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,
8581
actualExceptionMessage));
8682
}
8783

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Actions/ShouldThrowTestBuilder.cs renamed to src/MyTested.AspNetCore.Mvc.Abstractions/Builders/CaughtExceptions/ShouldThrowTestBuilder.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
1-
namespace MyTested.AspNetCore.Mvc.Builders.Actions
1+
namespace MyTested.AspNetCore.Mvc.Builders.CaughtExceptions
22
{
33
using System;
44
using Base;
5-
using Contracts.Actions;
6-
using Contracts.ExceptionErrors;
7-
using ExceptionErrors;
5+
using Contracts.CaughtExceptions;
86
using Exceptions;
97
using Internal.TestContexts;
10-
using Utilities.Extensions;
118

129
/// <summary>
13-
/// Used for testing whether action throws <see cref="System.Exception"/>.
10+
/// Used for testing whether method invocation throws <see cref="System.Exception"/>.
1411
/// </summary>
15-
public class ShouldThrowTestBuilder : BaseTestBuilderWithInvokedAction, IShouldThrowTestBuilder
12+
public class ShouldThrowTestBuilder : BaseTestBuilderWithComponent, IShouldThrowTestBuilder
1613
{
1714
private readonly IExceptionTestBuilder exceptionTestBuilder;
1815

1916
/// <summary>
2017
/// Initializes a new instance of the <see cref="ShouldThrowTestBuilder"/> class.
2118
/// </summary>
22-
/// <param name="testContext"><see cref="ControllerTestContext"/> containing data about the currently executed assertion chain.</param>
23-
public ShouldThrowTestBuilder(ControllerTestContext testContext)
19+
/// <param name="testContext"><see cref="ComponentTestContext"/> containing data about the currently executed assertion chain.</param>
20+
public ShouldThrowTestBuilder(ComponentTestContext testContext)
2421
: base(testContext)
2522
{
2623
this.exceptionTestBuilder = new ExceptionTestBuilder(this.TestContext);
@@ -36,15 +33,15 @@ public IExceptionTestBuilder Exception()
3633
public IAggregateExceptionTestBuilder AggregateException(int? withNumberOfInnerExceptions = null)
3734
{
3835
this.exceptionTestBuilder.OfType<AggregateException>();
39-
var aggregateException = this.CaughtException as AggregateException;
36+
var aggregateException = this.TestContext.CaughtException as AggregateException;
4037
var innerExceptionsCount = aggregateException.InnerExceptions.Count;
4138
if (withNumberOfInnerExceptions.HasValue &&
4239
withNumberOfInnerExceptions != innerExceptionsCount)
4340
{
4441
throw new InvalidExceptionAssertionException(string.Format(
45-
"When calling {0} action in {1} expected AggregateException to contain {2} inner exceptions, but in fact contained {3}.",
46-
this.ActionName,
47-
this.Controller.GetName(),
42+
"{0} {1} to contain {2} inner exceptions, but in fact contained {3}.",
43+
this.TestContext.ExceptionMessagePrefix,
44+
nameof(AggregateException),
4845
withNumberOfInnerExceptions,
4946
innerExceptionsCount));
5047
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.ExceptionErrors
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.CaughtExceptions
22
{
33
using System;
44

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.ExceptionErrors
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.CaughtExceptions
22
{
33
/// <summary>
44
/// Used for adding AndAlso() method to the <see cref="System.AggregateException"/> tests.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.ExceptionErrors
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.CaughtExceptions
22
{
33
/// <summary>
44
/// Used for adding AndAlso() method to the expected <see cref="System.Exception"/> tests.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.ExceptionErrors
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.CaughtExceptions
22
{
33
using Base;
44

55
/// <summary>
66
/// Used for testing expected <see cref="System.Exception"/> messages.
77
/// </summary>
8-
public interface IBaseExceptionTestBuilder : IBaseTestBuilderWithInvokedAction
8+
public interface IBaseExceptionTestBuilder : IBaseTestBuilderWithComponent
99
{
1010
/// <summary>
1111
/// Tests <see cref="System.Exception.Message"/> using test builder.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.ExceptionErrors
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.CaughtExceptions
22
{
33
using Base;
44

55
/// <summary>
66
/// Used for testing specific <see cref="System.Exception"/> messages.
77
/// </summary>
8-
public interface IExceptionMessageTestBuilder : IBaseTestBuilderWithInvokedAction
8+
public interface IExceptionMessageTestBuilder : IBaseTestBuilderWithComponent
99
{
1010
/// <summary>
1111
/// Tests whether the <see cref="System.Exception.Message"/> is equal to given message.

0 commit comments

Comments
 (0)