Skip to content

Commit

Permalink
Replace try/catch in tests with Should().Throw
Browse files Browse the repository at this point in the history
  • Loading branch information
jnyrup committed Mar 16, 2022
1 parent 1c0a20b commit 255cdfa
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 382 deletions.
36 changes: 12 additions & 24 deletions Tests/FluentAssertions.Specs/Exceptions/ExceptionAssertionSpecs.cs
Expand Up @@ -103,40 +103,28 @@ private static void EmptyAggregateException()
public void ThrowExactly_when_subject_throws_subclass_of_expected_exception_it_should_throw()
{
// Arrange
Action act = () => throw new ArgumentNullException();
Action subject = () => throw new ArgumentNullException();

try
{
// Act
act.Should().ThrowExactly<ArgumentException>("because {0} should do that", "Does.Do");
// Act
Action act = () => subject.Should().ThrowExactly<ArgumentException>("because {0} should do that", "Does.Do");

throw new XunitException("This point should not be reached.");
}
catch (XunitException ex)
{
// Assert
ex.Message.Should().Match("Expected type to be System.ArgumentException because Does.Do should do that, but found System.ArgumentNullException.");
}
// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected type to be System.ArgumentException because Does.Do should do that, but found System.ArgumentNullException.");
}

[Fact]
public void ThrowExactly_when_subject_throws_aggregate_exception_instead_of_expected_exception_it_should_throw()
{
// Arrange
Action act = () => throw new AggregateException(new ArgumentException());
Action subject = () => throw new AggregateException(new ArgumentException());

try
{
// Act
act.Should().ThrowExactly<ArgumentException>("because {0} should do that", "Does.Do");
// Act
Action act = () => subject.Should().ThrowExactly<ArgumentException>("because {0} should do that", "Does.Do");

throw new XunitException("This point should not be reached.");
}
catch (XunitException ex)
{
// Assert
ex.Message.Should().Match("Expected type to be System.ArgumentException because Does.Do should do that, but found System.AggregateException.");
}
// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected type to be System.ArgumentException because Does.Do should do that, but found System.AggregateException.");
}

[Fact]
Expand Down
152 changes: 51 additions & 101 deletions Tests/FluentAssertions.Specs/Exceptions/InnerExceptionSpecs.cs
Expand Up @@ -50,25 +50,16 @@ 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 subject = () => throw new BadImageFormatException("", innerException);

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

try
{
// Act
act.Should().Throw<BadImageFormatException>()
.WithInnerExceptionExactly<ArgumentException>();

throw new XunitException("This point should not be reached.");
}
catch (XunitException ex)
{
// Assert
var expectedMessage = BuildExpectedMessageForWithInnerExceptionExactly("Expected inner System.ArgumentException, but found System.ArgumentNullException with message", innerException.Message);
// Act
Action act = () => subject.Should().Throw<BadImageFormatException>()
.WithInnerExceptionExactly<ArgumentException>();

ex.Message.Should().Be(expectedMessage);
}
// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected*ArgumentException*found*ArgumentNullException*InnerExceptionMessage*");
}

[Fact]
Expand All @@ -86,25 +77,16 @@ 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 subject = () => throw new BadImageFormatException("", innerException);

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

try
{
// Act
act.Should().Throw<BadImageFormatException>()
.WithInnerExceptionExactly<ArgumentException>("because {0} should do just that", "the action");

throw new XunitException("This point should not be reached.");
}
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);
// Act
Action act = () => subject.Should().Throw<BadImageFormatException>()
.WithInnerExceptionExactly<ArgumentException>("because {0} should do just that", "the action");

ex.Message.Should().Be(expectedMessage);
}
// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected*ArgumentException*the action should do just that*ArgumentNullException*InnerExceptionMessage*");
}

[Fact]
Expand Down Expand Up @@ -133,25 +115,17 @@ 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();

Action act = () => throw new BadImageFormatException("", innerException);
var innerException = new ArgumentNullException("InnerExceptionMessage", (Exception)null);

try
{
// Act
act.Should().Throw<BadImageFormatException>()
.WithInnerExceptionExactly(typeof(ArgumentException), "because {0} should do just that", "the action");
Action subject = () => throw new BadImageFormatException("", innerException);

throw new XunitException("This point should not be reached.");
}
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);
// Act
Action act = () => subject.Should().Throw<BadImageFormatException>()
.WithInnerExceptionExactly(typeof(ArgumentException), "because {0} should do just that", "the action");

ex.Message.Should().Be(expectedMessage);
}
// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected*ArgumentException*the action should do just that*ArgumentNullException*InnerExceptionMessage*");
}

[Fact]
Expand All @@ -165,77 +139,53 @@ 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));

try
{
// Act
testSubject
.Invoking(x => x.Do())
.Should().Throw<Exception>()
.WithInnerException<ArgumentException>("because {0} should do just that", "Does.Do");

throw new XunitException("This point should not be reached");
}
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);
}
// Act
Action act = () => testSubject
.Invoking(x => x.Do())
.Should().Throw<Exception>()
.WithInnerException<ArgumentException>("because {0} should do just that", "Does.Do");

// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected*ArgumentException*Does.Do should do just that*NullReferenceException*InnerExceptionMessage*");
}

[Fact]
public void When_subject_throws_an_exception_without_expected_inner_exception_it_should_throw_with_clear_description()
{
try
{
Does testSubject = Does.Throw<Exception>();
// Arrange
Does testSubject = Does.Throw<Exception>();

testSubject.Invoking(x => x.Do()).Should().Throw<Exception>()
// Act
Action act = () => testSubject.Invoking(x => x.Do()).Should().Throw<Exception>()
.WithInnerException<InvalidOperationException>();

throw new XunitException("This point should not be reached");
}
catch (XunitException ex)
{
ex.Message.Should().Be(
"Expected inner System.InvalidOperationException, but the thrown exception has no inner exception.");
}
// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected inner System.InvalidOperationException, but the thrown exception has no inner exception.");
}

[Fact]
public void When_subject_throws_an_exception_without_expected_inner_exception_and_has_reason_it_should_throw_with_clear_description()
{
try
{
Does testSubject = Does.Throw<Exception>();

testSubject.Invoking(x => x.Do()).Should().Throw<Exception>()
.WithInnerException<InvalidOperationException>("because {0} should do that", "Does.Do");

throw new XunitException("This point should not be reached");
}
catch (XunitException ex)
{
ex.Message.Should().Be(
"Expected inner System.InvalidOperationException because Does.Do should do that, but the thrown exception has no inner exception.");
}
// Arrange
Does testSubject = Does.Throw<Exception>();

// Act
Action act = () => testSubject.Invoking(x => x.Do()).Should().Throw<Exception>()
.WithInnerException<InvalidOperationException>("because {0} should do that", "Does.Do");

// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected*InvalidOperationException*Does.Do should do that, but the thrown exception has no inner exception.");
}

[Fact]
Expand Down
Expand Up @@ -62,52 +62,33 @@ private static IEnumerable<char> MethodThatUsesYield(string bar)
public void When_custom_condition_is_not_met_it_should_throw()
{
// Arrange
Action act = () => throw new ArgumentException("");
Action subject = () => throw new ArgumentException("");

try
{
// Act
act
.Should().Throw<ArgumentException>("")
.Where(e => e.Message.Length > 0, "an exception must have a message");
// Act
Action act = () => subject
.Should().Throw<ArgumentException>("")
.Where(e => e.Message.Length > 0, "an exception must have a message");

throw new XunitException("This point should not be reached");
}
catch (XunitException exc)
{
// Assert
exc.Message.Should().StartWith(
"Expected exception where (e.Message.Length > 0) because an exception must have a message, but the condition was not met");
}
// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected exception where (e.Message.Length > 0) because an exception must have a message, but the condition was not met*");
}

[Fact]
public void When_a_2nd_condition_is_not_met_it_should_throw()
{
// Arrange
Action act = () => throw new ArgumentException("Fail");
Action subject = () => throw new ArgumentException("Fail");

try
{
// Act
act
.Should().Throw<ArgumentException>("")
.Where(e => e.Message.Length > 0)
.Where(e => e.Message == "Error");
// Act
Action act = () => subject
.Should().Throw<ArgumentException>("")
.Where(e => e.Message.Length > 0)
.Where(e => e.Message == "Error");

throw new XunitException("This point should not be reached");
}
catch (XunitException exc)
{
// Assert
exc.Message.Should().StartWith(
"Expected exception where (e.Message == \"Error\"), but the condition was not met");
}
catch (Exception exc)
{
exc.Message.Should().StartWith(
"Expected exception where (e.Message == \"Error\"), but the condition was not met");
}
// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected exception where (e.Message == \"Error\"), but the condition was not met*");
}

[Fact]
Expand Down

0 comments on commit 255cdfa

Please sign in to comment.