Skip to content

Commit f45cf70

Browse files
committed
Added ValidateInvocationResultTypes to the InvocationResultValidator (#359)
1 parent afa50dd commit f45cf70

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/Validators/InvocationResultValidator.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace MyTested.AspNetCore.Mvc.Utilities.Validators
22
{
33
using System;
4+
using System.Linq;
45
using System.Reflection;
56
using Exceptions;
67
using Extensions;
@@ -41,6 +42,43 @@ public static void ValidateInvocationResultType<TExpectedType>(
4142
bool allowDifferentGenericTypeDefinitions = false)
4243
=> ValidateInvocationResultType(testContext, typeof(TExpectedType), canBeAssignable, allowDifferentGenericTypeDefinitions);
4344

45+
public static void ValidateInvocationResultTypes(
46+
ComponentTestContext testContext,
47+
bool canBeAssignable = false,
48+
bool allowDifferentGenericTypeDefinitions = false,
49+
params Type[] typesOfExpectedReturnValue)
50+
{
51+
var invalid = false;
52+
53+
foreach (var type in typesOfExpectedReturnValue)
54+
{
55+
invalid = InvocationResultTypeIsInvalid(
56+
testContext,
57+
type,
58+
canBeAssignable,
59+
allowDifferentGenericTypeDefinitions);
60+
61+
if (!invalid)
62+
{
63+
break;
64+
}
65+
}
66+
67+
if (invalid)
68+
{
69+
var expectedTypeName = string.Join(
70+
" or ",
71+
typesOfExpectedReturnValue.Select(t => t.ToFriendlyTypeName()));
72+
73+
var actualTypeName = testContext.MethodResult?.GetType().ToFriendlyTypeName();
74+
75+
ThrowNewInvocationResultAssertionException(
76+
testContext,
77+
expectedTypeName,
78+
actualTypeName);
79+
}
80+
}
81+
4482
public static void ValidateInvocationResult<TResult>(ComponentTestContext testContext, TResult model, bool canBeAssignable = false)
4583
{
4684
if (!Reflection.IsAnonymousType(typeof(TResult)))

src/MyTested.AspNetCore.Mvc.Pipeline/Builders/Pipeline/WhichControllerInstanceBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public IBaseTestBuilderWithInvokedAction ShouldReturnEmpty()
4747
.ShouldReturnEmpty();
4848

4949
/// <inheritdoc />
50-
public IShouldReturnTestBuilder<MethodResult> ShouldReturn()
50+
public IShouldReturnActionResultTestBuilder<MethodResult> ShouldReturn()
5151
{
5252
this.InvokeAction();
5353

0 commit comments

Comments
 (0)