Skip to content

Commit

Permalink
Issue #756 Return warning message from BecauseOf when the phrase cann…
Browse files Browse the repository at this point in the history
…ot be formatted (#764)
  • Loading branch information
bernard-chen authored and dennisdoomen committed Feb 15, 2018
1 parent 6d49c00 commit c493c4a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
20 changes: 14 additions & 6 deletions Src/FluentAssertions/Execution/AssertionScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,26 @@ public AssertionScope UsingLineBreaks
/// <param name="because">
/// A formatted phrase compatible with <see cref="string.Format(string,object[])"/> explaining why
/// the condition should be satisfied. If the phrase does not start with the word <i>because</i>,
/// it is prepended to the message.
/// it is prepended to the message. If the format of <paramref name="because"/> or
/// <paramref name="becauseArgs"/> is not compatible with <see cref="string.Format(string,object[])"/>,
/// then a warning message is returned instead.
/// </param>
/// <param name="becauseArgs">
/// Zero or more values to use for filling in any <see cref="string.Format(string,object[])"/> compatible placeholders.
/// </param>
/// <exception cref="System.FormatException">
/// Thrown if the format of <paramref name="because"/> or <paramref name="becauseArgs"/> is not
/// compatible with <see cref="string.Format(string,object[])"/>.
/// </exception>
public AssertionScope BecauseOf(string because, params object[] becauseArgs)
{
reason = () => string.Format(because ?? "", becauseArgs ?? new object[0]);
reason = () =>
{
try
{
return string.Format(because ?? "", becauseArgs ?? new object[0]);
}
catch (FormatException formatException)
{
return $"**WARNING** because message '{because}' could not be formatted with string.Format{Environment.NewLine}{formatException.StackTrace}";
}
};
return this;
}

Expand Down
15 changes: 15 additions & 0 deletions Tests/Shared.Specs/AssertionScopeSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ public void When_parentheses_are_used_in_the_because_arguments_it_should_render_
.WithMessage("*because can't use these in becauseArgs: { }*");
}

[Fact]
public void When_invalid_format_is_used_in_because_parameter_it_should_render_default_text()
{
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => 1.Should().Be(2, "use of {} is considered invalid in because parameter");

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage("*because message 'use of {} is considered invalid in because parameter' could not be formatted with string.Format*");
}

[Fact]
public void When_an_assertion_fails_in_a_scope_with_braces_it_should_use_the_name_as_the_assertion_context()
{
Expand Down

0 comments on commit c493c4a

Please sign in to comment.