Skip to content

Commit f4a7f44

Browse files
committed
Documented all view component testing methods and interfaces (#66)
1 parent c747832 commit f4a7f44

File tree

39 files changed

+603
-163
lines changed

39 files changed

+603
-163
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Base;
77
using Internal.TestContexts;
88
using Utilities;
9+
using Exceptions;
10+
using Utilities.Extensions;
911

1012
/// <summary>
1113
/// Base class for all attribute test builders.
@@ -34,7 +36,7 @@ internal ICollection<Action<IEnumerable<object>>> GetAttributeValidations()
3436
}
3537

3638
/// <summary>
37-
/// Tests whether the action attributes contain the provided attribute type.
39+
/// Tests whether the attributes contain the provided attribute type.
3840
/// </summary>
3941
/// <typeparam name="TAttribute">Type of expected attribute.</typeparam>
4042
/// <param name="failedValidationAction">Action to execute, if the validation fails.</param>
@@ -76,5 +78,14 @@ protected TAttribute TryGetAttributeOfType<TAttribute>(IEnumerable<object> attri
7678
{
7779
return attributes.FirstOrDefault(a => a.GetType() == typeof(TAttribute)) as TAttribute;
7880
}
81+
82+
protected virtual void ThrowNewAttributeAssertionException(string expectedValue, string actualValue)
83+
{
84+
throw new AttributeAssertionException(string.Format(
85+
"When testing {0} was expected to have {1}, but {2}.",
86+
this.TestContext.Component.GetName(),
87+
expectedValue,
88+
actualValue));
89+
}
7990
}
8091
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Attributes
2+
{
3+
using System;
4+
5+
public interface IBaseAttributesTestBuilder<TAttributesTestBuilder>
6+
{
7+
/// <summary>
8+
/// Checks whether the collected attributes contain the provided attribute type.
9+
/// </summary>
10+
/// <typeparam name="TAttribute">Type of expected attribute.</typeparam>
11+
/// <returns>The same attributes test builder.</returns>
12+
TAttributesTestBuilder ContainingAttributeOfType<TAttribute>()
13+
where TAttribute : Attribute;
14+
}
15+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Invocations
2+
{
3+
using System;
4+
using Models;
5+
6+
public interface IBaseShouldReturnTestBuilder<TInvocationResult, TBuilder>
7+
{
8+
/// <summary>
9+
/// Tests whether the result is the default value of the type.
10+
/// </summary>
11+
/// <returns>Base test builder.</returns>
12+
TBuilder DefaultValue();
13+
14+
/// <summary>
15+
/// Tests whether the result is null.
16+
/// </summary>
17+
/// <returns>Base test builder.</returns>
18+
TBuilder Null();
19+
20+
/// <summary>
21+
/// Tests whether the result is not null.
22+
/// </summary>
23+
/// <returns>Base test builder.</returns>
24+
TBuilder NotNull();
25+
26+
/// <summary>
27+
/// Tests whether the result is of the provided type.
28+
/// </summary>
29+
/// <typeparam name="TResult">Expected response type.</typeparam>
30+
/// <returns>Test builder of <see cref="IModelDetailsTestBuilder{TInvocationResult}"/> type.</returns>
31+
IAndModelDetailsTestBuilder<TInvocationResult> ResultOfType<TResult>();
32+
33+
/// <summary>
34+
/// Tests whether the result is of the provided type.
35+
/// </summary>
36+
/// <param name="returnType">Expected return type.</param>
37+
/// <returns>Test builder of <see cref="IModelDetailsTestBuilder{TInvocationResult}"/> type.</returns>
38+
IAndModelDetailsTestBuilder<TInvocationResult> ResultOfType(Type returnType);
39+
40+
/// <summary>
41+
/// Tests whether the result is deeply equal to the provided one.
42+
/// </summary>
43+
/// <typeparam name="TResult">Expected response type.</typeparam>
44+
/// <param name="model">Expected return object.</param>
45+
/// <returns>Test builder of <see cref="IModelDetailsTestBuilder{TInvocationResult}"/> type.</returns>
46+
IAndModelDetailsTestBuilder<TInvocationResult> Result<TResult>(TResult model);
47+
48+
}
49+
}

src/MyTested.AspNetCore.Mvc.Abstractions/Plugins/AbstractionsTestPlugin.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
using Internal.TestContexts;
66
using Microsoft.Extensions.DependencyInjection;
77
using Utilities;
8-
using Microsoft.AspNetCore.Http;
98

109
public class AbstractionsTestPlugin : BaseTestPlugin, IServiceRegistrationPlugin, IInitializationPlugin, IShouldPassForPlugin
1110
{
12-
private readonly Type baseHttpContextType = typeof(HttpContext);
13-
private readonly Type baseHttpRequestType = typeof(HttpRequest);
11+
private readonly Type exceptionType = typeof(Exception);
1412

1513
public Action<IServiceCollection> ServiceRegistrationDelegate =>
1614
serviceCollection => serviceCollection.AddCoreTesting();
@@ -19,10 +17,8 @@ public class AbstractionsTestPlugin : BaseTestPlugin, IServiceRegistrationPlugin
1917
public Action<IServiceProvider> InitializationDelegate => serviceProvider => serviceProvider.GetService<IControllerActionDescriptorCache>();
2018

2119
public object TryGetValue(Type type, ComponentTestContext testContext)
22-
=> Reflection.AreAssignable(baseHttpContextType, type) // HttpContext
23-
? testContext.HttpContext
24-
: Reflection.AreAssignable(baseHttpRequestType, type) // HttpRequest
25-
? (object)testContext.HttpRequest
20+
=> Reflection.AreAssignable(exceptionType, type) // Exception
21+
? testContext.CaughtException
2622
: null;
2723
}
2824
}

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Actions/ShouldReturn/ShouldReturnNullOrDefault.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public IBaseTestBuilderWithActionResult<TActionResult> DefaultValue()
2020
"the default value of {0}, but in fact it was not.",
2121
typeof(TActionResult).ToFriendlyTypeName()));
2222
}
23-
23+
2424
return this.NewAndTestBuilderWithActionResult();
2525
}
2626

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Attributes/ActionAttributesTestBuilder.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,11 @@ public IActionAttributesTestBuilder AndAlso()
158158
return this;
159159
}
160160

161-
private void ThrowNewAttributeAssertionException(string expectedValue, string actualValue)
161+
protected override void ThrowNewAttributeAssertionException(string expectedValue, string actualValue)
162162
{
163163
throw new AttributeAssertionException(string.Format(
164-
"When calling {0} action in {1} expected action to have {2}, but {3}.",
165-
this.TestContext.MethodName,
166-
this.TestContext.Component.GetName(),
164+
"{0} action to have {1}, but {2}.",
165+
this.TestContext.ExceptionMessagePrefix,
167166
expectedValue,
168167
actualValue));
169168
}

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Attributes/ControllerActionAttributesTestBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/// <summary>
1010
/// Base class for controller action test builders.
1111
/// </summary>
12-
public class ControllerActionAttributesTestBuilder : BaseAttributesTestBuilder
12+
public abstract class ControllerActionAttributesTestBuilder : BaseAttributesTestBuilder
1313
{
1414
/// <summary>
1515
/// Initializes a new instance of the <see cref="ControllerActionAttributesTestBuilder"/> class.

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Attributes/ControllerAttributesTestBuilder.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
{
33
using System;
44
using Contracts.Attributes;
5-
using Exceptions;
65
using Internal.TestContexts;
76
using Microsoft.AspNetCore.Authorization;
8-
using Utilities.Extensions;
97

108
/// <summary>
119
/// Used for testing controller attributes.
@@ -66,14 +64,5 @@ public IControllerAttributesTestBuilder AndAlso()
6664
{
6765
return this;
6866
}
69-
70-
private void ThrowNewAttributeAssertionException(string expectedValue, string actualValue)
71-
{
72-
throw new AttributeAssertionException(string.Format(
73-
"When testing {0} was expected to have {1}, but {2}.",
74-
this.TestContext.Component.GetName(),
75-
expectedValue,
76-
actualValue));
77-
}
7867
}
7968
}

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Contracts/Actions/IShouldReturnTestBuilder.cs

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,54 +15,15 @@
1515
using ActionResults.Redirect;
1616
using ActionResults.StatusCode;
1717
using Base;
18-
using Models;
18+
using Invocations;
1919

2020
/// <summary>
2121
/// Used for testing returned action result.
2222
/// </summary>
2323
/// <typeparam name="TActionResult">Result from invoked action in ASP.NET Core MVC controller.</typeparam>
24-
public interface IShouldReturnTestBuilder<TActionResult>
24+
public interface IShouldReturnTestBuilder<TActionResult>
25+
: IBaseShouldReturnTestBuilder<TActionResult, IBaseTestBuilderWithActionResult<TActionResult>>
2526
{
26-
/// <summary>
27-
/// Tests whether the action result is the default value of the type.
28-
/// </summary>
29-
/// <returns>Test builder of <see cref="IBaseTestBuilderWithActionResult{TActionResult}"/> type.</returns>
30-
IBaseTestBuilderWithActionResult<TActionResult> DefaultValue();
31-
32-
/// <summary>
33-
/// Tests whether the action result is null.
34-
/// </summary>
35-
/// <returns>Test builder of <see cref="IBaseTestBuilderWithActionResult{TActionResult}"/> type.</returns>
36-
IBaseTestBuilderWithActionResult<TActionResult> Null();
37-
38-
/// <summary>
39-
/// Tests whether the action result is not null.
40-
/// </summary>
41-
/// <returns>Test builder of <see cref="IBaseTestBuilderWithActionResult{TActionResult}"/> type.</returns>
42-
IBaseTestBuilderWithActionResult<TActionResult> NotNull();
43-
44-
/// <summary>
45-
/// Tests whether the action result is of the provided type.
46-
/// </summary>
47-
/// <typeparam name="TResult">Expected response type.</typeparam>
48-
/// <returns>Test builder of <see cref="IModelDetailsTestBuilder{TActionResult}"/> type.</returns>
49-
IAndModelDetailsTestBuilder<TActionResult> ResultOfType<TResult>();
50-
51-
/// <summary>
52-
/// Tests whether the action result is of the provided type.
53-
/// </summary>
54-
/// <param name="returnType">Expected return type.</param>
55-
/// <returns>Test builder of <see cref="IModelDetailsTestBuilder{TActionResult}"/> type.</returns>
56-
IAndModelDetailsTestBuilder<TActionResult> ResultOfType(Type returnType);
57-
58-
/// <summary>
59-
/// Tests whether the action result is deeply equal to the provided one.
60-
/// </summary>
61-
/// <typeparam name="TResult">Expected response type.</typeparam>
62-
/// <param name="model">Expected return object.</param>
63-
/// <returns>Test builder of <see cref="IModelDetailsTestBuilder{TActionResult}"/> type.</returns>
64-
IAndModelDetailsTestBuilder<TActionResult> Result<TResult>(TResult model);
65-
6627
/// <summary>
6728
/// Tests whether the action result is <see cref="Microsoft.AspNetCore.Mvc.ChallengeResult"/>.
6829
/// </summary>

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Contracts/Attributes/IActionAttributesTestBuilder.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,8 @@
99
/// <summary>
1010
/// Used for testing action attributes.
1111
/// </summary>
12-
public interface IActionAttributesTestBuilder
12+
public interface IActionAttributesTestBuilder : IBaseAttributesTestBuilder<IAndActionAttributesTestBuilder>
1313
{
14-
/// <summary>
15-
/// Tests whether the action attributes contain the provided attribute type.
16-
/// </summary>
17-
/// <typeparam name="TAttribute">Type of expected attribute.</typeparam>
18-
/// <returns>The same <see cref="IAndActionAttributesTestBuilder"/>.</returns>
19-
IAndActionAttributesTestBuilder ContainingAttributeOfType<TAttribute>()
20-
where TAttribute : Attribute;
21-
2214
/// <summary>
2315
/// Tests whether the action attributes contain <see cref="Microsoft.AspNetCore.Mvc.ActionNameAttribute"/>.
2416
/// </summary>

0 commit comments

Comments
 (0)