Skip to content

Commit 0a7bab0

Browse files
committed
Add .Passing() tests for ChallengeTestBuilder
1 parent c281dd6 commit 0a7bab0

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test/BuildersTests/ActionResultsTests/ChallengeTests/ChallengeTestBuilderTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Exceptions;
55
using Setups;
66
using Setups.Controllers;
7+
using Utilities;
78
using Xunit;
89

910
public class ChallengeTestBuilderTests
@@ -140,5 +141,58 @@ public void AndAlsoShouldWorkCorrectly()
140141
.AndAlso()
141142
.ContainingAuthenticationScheme(AuthenticationScheme.NTLM));
142143
}
144+
145+
146+
[Fact]
147+
public void PassingShouldCorrectlyRunItsAssertionFunction()
148+
{
149+
MyController<MvcController>
150+
.Instance()
151+
.Calling(c => c.ChallengeWithAuthenticationSchemes())
152+
.ShouldReturn()
153+
.Challenge(challenge => challenge
154+
.Passing(c => c.AuthenticationSchemes?.Count == 2));
155+
}
156+
157+
[Fact]
158+
public void PassingShouldThrowAnExceptionOnAnIncorrectAssertion()
159+
{
160+
Test.AssertException<InvocationResultAssertionException>(
161+
() =>
162+
{
163+
MyController<MvcController>
164+
.Instance()
165+
.Calling(c => c.ChallengeWithAuthenticationSchemes())
166+
.ShouldReturn()
167+
.Challenge(challenge => challenge
168+
.Passing(c => c.AuthenticationSchemes?.Count == 0));
169+
},
170+
$"When calling {nameof(MvcController.ChallengeWithAuthenticationSchemes)} " +
171+
$"action in {nameof(MvcController)} expected the ChallengeResult to pass the given predicate, but it failed.");
172+
}
173+
174+
[Fact]
175+
public void PassingShouldCorrectlyRunItsAssertionAction()
176+
{
177+
MyController<MvcController>
178+
.Instance()
179+
.Calling(c => c.ChallengeWithAuthenticationSchemes())
180+
.ShouldReturn()
181+
.Challenge(challenge => challenge
182+
.Passing(c =>
183+
{
184+
const int expectedAuthSchemesCount = 2;
185+
var actualAuthSchemesCount = c.AuthenticationSchemes?.Count;
186+
if (actualAuthSchemesCount != expectedAuthSchemesCount)
187+
{
188+
throw new InvalidAssertionException(
189+
string.Format("Expected {0} to have {1} {2}, but it has {3}.",
190+
c.GetType().ToFriendlyTypeName(),
191+
expectedAuthSchemesCount,
192+
nameof(c.AuthenticationSchemes),
193+
actualAuthSchemesCount));
194+
};
195+
}));
196+
}
143197
}
144198
}

0 commit comments

Comments
 (0)