-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created an Assertion for StatusCodeShouldBeSuccess (#161)
* Created an Assertion for StatusCodeShouldBeSuccess * Rebase --------- Co-authored-by: JT <Hawxy@users.noreply.github.com>
- Loading branch information
1 parent
fbdc3bc
commit c99ee6d
Showing
4 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/Alba.Testing/Assertions/StatusCodeSuccessAssertionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Alba.Assertions; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Alba.Testing.Assertions | ||
{ | ||
public class StatusCodeSuccessAssertionTests | ||
{ | ||
[Theory] | ||
[ClassData(typeof(SuccessStatusCodes))] | ||
public void HappyPath(int statusCode) | ||
{ | ||
var assertion = new StatusCodeSuccessAssertion(); | ||
|
||
AssertionRunner.Run(assertion, _ => _.StatusCode(statusCode)) | ||
.AssertAll(); | ||
} | ||
|
||
[Theory] | ||
[ClassData(typeof(FailureStatusCodes))] | ||
public void SadPath(int statusCode) | ||
{ | ||
var assertion = new StatusCodeSuccessAssertion(); | ||
|
||
AssertionRunner.Run(assertion, _ => _.StatusCode(statusCode)) | ||
.SingleMessageShouldBe($"Expected a status code between 200 and 299, but was {statusCode}"); | ||
} | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
public class SuccessStatusCodes : IEnumerable<object[]> | ||
{ | ||
public IEnumerator<object[]> GetEnumerator() | ||
{ | ||
foreach (var code in Enumerable.Range(200, 99)) | ||
{ | ||
yield return new object[] { code }; | ||
} | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
|
||
} | ||
|
||
public class FailureStatusCodes : IEnumerable<object[]> | ||
{ | ||
public IEnumerator<object[]> GetEnumerator() | ||
{ | ||
foreach (var code in Enumerable.Range(100, 99)) | ||
{ | ||
yield return new object[] { code }; | ||
} | ||
foreach (var code in Enumerable.Range(300, 200)) | ||
{ | ||
yield return new object[] { code }; | ||
} | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace Alba.Assertions; | ||
|
||
public sealed class StatusCodeSuccessAssertion : IScenarioAssertion | ||
{ | ||
public void Assert(Scenario scenario, AssertionContext context) | ||
{ | ||
var statusCode = context.HttpContext.Response.StatusCode; | ||
if(statusCode < 200 || statusCode >= 300) | ||
{ | ||
context.AddFailure($"Expected a status code between 200 and 299, but was {statusCode}"); | ||
context.ReadBodyAsString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters