Skip to content

Commit

Permalink
Merge pull request #1853 from jnyrup/remove_try_catch_from_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jnyrup committed Apr 3, 2022
2 parents 51b0232 + 063fc89 commit f9b57ae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 40 deletions.
33 changes: 9 additions & 24 deletions Tests/FluentAssertions.Specs/Exceptions/InnerExceptionSpecs.cs
Expand Up @@ -50,7 +50,7 @@ public void When_subject_throws_an_exception_with_the_expected_inner_exception_f
public void WithInnerExceptionExactly_no_parameters_when_subject_throws_subclass_of_expected_inner_exception_it_should_throw_with_clear_description()
{
// Arrange
var innerException = new ArgumentNullException();
var innerException = new ArgumentNullException("InnerExceptionMessage", (Exception)null);

Action act = () => throw new BadImageFormatException("", innerException);

Expand All @@ -65,9 +65,7 @@ public void WithInnerExceptionExactly_no_parameters_when_subject_throws_subclass
catch (XunitException ex)
{
// Assert
var expectedMessage = BuildExpectedMessageForWithInnerExceptionExactly("Expected inner System.ArgumentException, but found System.ArgumentNullException with message", innerException.Message);

ex.Message.Should().Be(expectedMessage);
ex.Message.Should().Match("Expected*ArgumentException*found*ArgumentNullException*InnerExceptionMessage*");
}
}

Expand All @@ -86,7 +84,7 @@ public void WithInnerExceptionExactly_no_parameters_when_subject_throws_expected
public void WithInnerExceptionExactly_when_subject_throws_subclass_of_expected_inner_exception_it_should_throw_with_clear_description()
{
// Arrange
var innerException = new ArgumentNullException();
var innerException = new ArgumentNullException("InnerExceptionMessage", (Exception)null);

Action act = () => throw new BadImageFormatException("", innerException);

Expand All @@ -101,9 +99,7 @@ public void WithInnerExceptionExactly_when_subject_throws_subclass_of_expected_i
catch (XunitException ex)
{
// Assert
var expectedMessage = BuildExpectedMessageForWithInnerExceptionExactly("Expected inner System.ArgumentException because the action should do just that, but found System.ArgumentNullException with message", innerException.Message);

ex.Message.Should().Be(expectedMessage);
ex.Message.Should().Match("Expected*ArgumentException*the action should do just that*ArgumentNullException*InnerExceptionMessage*");
}
}

Expand Down Expand Up @@ -133,7 +129,7 @@ public void WithInnerExceptionExactly_with_type_exception_no_parameters_when_sub
public void WithInnerExceptionExactly_with_type_exception_when_subject_throws_subclass_of_expected_inner_exception_it_should_throw_with_clear_description()
{
// Arrange
var innerException = new ArgumentNullException();
var innerException = new ArgumentNullException("InnerExceptionMessage", (Exception)null);

Action act = () => throw new BadImageFormatException("", innerException);

Expand All @@ -148,9 +144,7 @@ public void WithInnerExceptionExactly_with_type_exception_when_subject_throws_su
catch (XunitException ex)
{
// Assert
var expectedMessage = BuildExpectedMessageForWithInnerExceptionExactly("Expected inner System.ArgumentException because the action should do just that, but found System.ArgumentNullException with message", innerException.Message);

ex.Message.Should().Be(expectedMessage);
ex.Message.Should().Match("Expected*ArgumentException*the action should do just that*ArgumentNullException*InnerExceptionMessage*");
}
}

Expand All @@ -165,18 +159,11 @@ public void WithInnerExceptionExactly_when_subject_throws_expected_inner_excepti
.WithInnerExceptionExactly<ArgumentNullException>("because {0} should do just that", "the action");
}

private static string BuildExpectedMessageForWithInnerExceptionExactly(string because, string innerExceptionMessage)
{
var expectedMessage = $"{because} \"{innerExceptionMessage}\".";

return expectedMessage;
}

[Fact]
public void When_subject_throws_an_exception_with_an_unexpected_inner_exception_it_should_throw_with_clear_description()
{
// Arrange
var innerException = new NullReferenceException();
var innerException = new NullReferenceException("InnerExceptionMessage");

Does testSubject = Does.Throw(new Exception("", innerException));

Expand All @@ -193,10 +180,8 @@ public void When_subject_throws_an_exception_with_an_unexpected_inner_exception_
catch (XunitException exc)
{
// Assert
exc.Message.Should().StartWith(
"Expected inner System.ArgumentException because Does.Do should do just that, but found System.NullReferenceException");

exc.Message.Should().Contain(innerException.Message);
exc.Message.Should().Match(
"Expected*ArgumentException*Does.Do should do just that*NullReferenceException*InnerExceptionMessage*");
}
}

Expand Down
Expand Up @@ -31,16 +31,8 @@ public void When_the_same_failure_is_handled_twice_or_more_it_should_still_repor
Action act = scope.Dispose;

// Assert
try
{
act();
}
catch (Exception exception)
{
int matches = new Regex(".*Failure.*").Matches(exception.Message).Count;

matches.Should().Be(4);
}
act.Should().Throw<XunitException>()
.Which.Message.Should().Contain("Failure", Exactly.Times(4));
}

[InlineData("foo")]
Expand Down
9 changes: 3 additions & 6 deletions Tests/FluentAssertions.Specs/Execution/AssertionScopeSpecs.cs
Expand Up @@ -132,9 +132,7 @@ public void When_multiple_scopes_are_nested_it_should_throw_all_failures_from_th
}
catch (Exception exception)
{
exception.Message.Should().Contain("Failure1");
exception.Message.Should().Contain("Failure2");
exception.Message.Should().Contain("Failure3");
exception.Message.Should().ContainAll("Failure1", "Failure2", "Failure3");
}
}

Expand Down Expand Up @@ -165,9 +163,8 @@ public void When_a_nested_scope_is_discarded_its_failures_should_also_be_discard
}
catch (Exception exception)
{
exception.Message.Should().Contain("Failure1");
exception.Message.Should().Contain("Failure2");
exception.Message.Should().NotContain("Failure3");
exception.Message.Should().ContainAll("Failure1", "Failure2")
.And.NotContain("Failure3");
}
}

Expand Down

0 comments on commit f9b57ae

Please sign in to comment.