Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Hawxy committed Jul 26, 2024
1 parent 02a46ab commit 941c6d7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 8 additions & 6 deletions docs/scenarios/assertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ failures will be reported out in the Exception message thrown by Alba on Scenari
:::

The Scenario assertions in Alba are completely extensible and you can happily add your own via extension methods - but
please send anything that's generally useful as a pull request to Alba itself;-)
please send anything that's generally useful as a pull request to Alba itself ;-)

The first step is to write your own implementation of this interface:

Expand All @@ -15,7 +15,7 @@ The first step is to write your own implementation of this interface:
```cs
public interface IScenarioAssertion
{
void Assert(Scenario scenario, HttpContext context, ScenarioAssertionException ex);
void Assert(Scenario scenario, AssertionContext context);
}
```
<sup><a href='https://github.com/JasperFx/alba/blob/master/src/Alba/IScenarioAssertion.cs#L5-L10' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_iscenarioassertion' title='Start of snippet'>anchor</a></sup>
Expand All @@ -35,19 +35,21 @@ internal sealed class BodyContainsAssertion : IScenarioAssertion
Text = text;
}

public void Assert(Scenario scenario, HttpContext context, ScenarioAssertionException ex)
public void Assert(Scenario scenario, AssertionContext context)
{
var body = ex.ReadBody(context);
// Context has this useful extension to read the body as a string.
// This will bake the body contents into the exception message to make debugging easier.
var body = context.ReadBodyAsString();
if (!body.Contains(Text))
{
// Add the failure message to the exception. This exception only
// gets thrown if there are failures.
ex.Add($"Expected text '{Text}' was not found in the response body");
context.AddFailure($"Expected text '{Text}' was not found in the response body");
}
}
}
```
<sup><a href='https://github.com/JasperFx/alba/blob/master/src/Alba/Assertions/BodyContainsAssertion.cs#L5-L26' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_bodycontainsassertion' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/alba/blob/master/src/Alba/Assertions/BodyContainsAssertion.cs#L5-L28' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_bodycontainsassertion' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Once you have your assertion class, you can apply it to a scenario through an extension method against the
Expand Down
2 changes: 2 additions & 0 deletions src/Alba/Assertions/BodyContainsAssertion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public BodyContainsAssertion(string text)

public void Assert(Scenario scenario, AssertionContext context)
{
// Context has this useful extension to read the body as a string.
// This will bake the body contents into the exception message to make debugging easier.
var body = context.ReadBodyAsString();
if (!body.Contains(Text))
{
Expand Down

0 comments on commit 941c6d7

Please sign in to comment.