Skip to content

Commit

Permalink
Add ThrowExactlyAsync and appropriate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krajek committed Sep 30, 2018
1 parent 2e12925 commit d2de932
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Src/FluentAssertions/AssertionExtensions.Actions.cs
Expand Up @@ -64,6 +64,32 @@ public static partial class AssertionExtensions
return exceptionAssertions;
}

/// <summary>
/// Asserts that the <paramref name="asyncActionAssertions"/> subject throws the exact exception (and not a derived exception type).
/// </summary>
/// <param name="asyncActionAssertions">A reference to the method or property.</param>
/// <typeparam name="TException">
/// The type of the exception it should throw.
/// </typeparam>
/// <param name="because">
/// A formatted phrase explaining why the assertion should be satisfied. If the phrase does not
/// start with the word <i>because</i>, it is prepended to the message.
/// </param>
/// <param name="becauseArgs">
/// Zero or more values to use for filling in any <see cref="string.Format(string,object[])"/> compatible placeholders.
/// </param>
/// <returns>
/// Returns an object that allows asserting additional members of the thrown exception.
/// </returns>
public static async Task<ExceptionAssertions<TException>> ThrowExactlyAsync<TException>(this AsyncFunctionAssertions asyncActionAssertions, string because = "",
params object[] becauseArgs)
where TException : Exception
{
var exceptionAssertions = await asyncActionAssertions.ThrowAsync<TException>(because, becauseArgs);
exceptionAssertions.Which.GetType().Should().Be<TException>(because, becauseArgs);
return exceptionAssertions;
}

private class AggregateExceptionExtractor : IExtractExceptions
{
public IEnumerable<T> OfType<T>(Exception actualException)
Expand Down
58 changes: 58 additions & 0 deletions Tests/Shared.Specs/AsyncFunctionExceptionAssertionSpecs.cs
Expand Up @@ -171,6 +171,64 @@ public async Task When_async_method_does_not_throw_async_exception_and_that_was_
await action.Should().NotThrowAsync();
}

[Fact]
public async Task When_subject_throws_subclass_of_expected_async_exception_it_should_succeed()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var asyncObject = new AsyncClass();

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Func<Task> action = async () => await asyncObject.ThrowAsync<ArgumentNullException>();

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
await action.Should().ThrowAsync<ArgumentException>("because {0} should do that", "IFoo.Do");
}

[Fact]
public async Task When_subject_throws_subclass_of_expected_async_exact_exception_it_should_throw()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var asyncObject = new AsyncClass();

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Func<Task> action = async () => await asyncObject.ThrowAsync<ArgumentNullException>();
Func<Task> testAction = async () => await action.Should().ThrowExactlyAsync<ArgumentException>("ABCDE");

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
(await testAction.Should().ThrowAsync<XunitException>()).WithMessage("*ArgumentException*ABCDE*ArgumentNullException*");
}

[Fact]
public async Task When_subject_throws_expected_async_exact_exception_it_should_succeed()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var asyncObject = new AsyncClass();

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Func<Task> action = async () => await asyncObject.ThrowAsync<ArgumentException>();

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
await action.Should().ThrowExactlyAsync<ArgumentException>("because {0} should do that", "IFoo.Do");
}

[Fact]
public void When_async_method_throws_exception_and_no_exception_was_expected_it_should_fail()
{
Expand Down

0 comments on commit d2de932

Please sign in to comment.