Skip to content

ThrowsAsync<T> doesn't ensure that the thrown Exception is of type T #2538

@jackofcoding

Description

@jackofcoding

Preamble

The current latest version (0.25.6) suffers from this, as does 0.22.31, but version 0.22.24 does not suffer from this.
It therefore seems to have been introduced with the 0.22.31 release.
I would assume that #2479 is the cause of this.

Reproduction

public class Foo
{
  private void ThrowsArgumentException() => throw new ArgumentException("Just for testing");

  [Test]
  public async Task ThrowsAsync_DoesNotCheckType()
  {
    var exception = await Assert.ThrowsAsync<OverflowException>(LocalTestFunction);
    await Assert.That(exception).IsNotNull();
    await Assert.That(exception.Message).IsEqualTo("Just for testing");
    await Assert.That(exception).IsTypeOf<OverflowException>();
    return;

    Task LocalTestFunction()
    {
      ThrowsArgumentException();
      return Task.CompletedTask;
    }
  }
}

Result with 0.22.24

AssertionException: Exception is of type ArgumentException instead of OverflowException for LocalTestFunction
Exception doesn't have a stacktrace

Result with 0.22.31 onwards (including 0.25.6)

AssertionException: Expected OverflowException but received ArgumentException
Exception doesn't have a stacktrace

Expectations

  1. I think it's fair to expect that exception will be of type OverflowException or the method fails.
    This is an assertion after all, and I don't expect exception to actually be ArgumentException!
  2. As you can also see with version 0.22.31 onwards, it's the custom assertion on type that is the one that fails.
    When using generics, you expect that type, so this assertion shouldn't even have to be there, I guess.
  3. Some documentation around ThrowsAsync, as I couldn't find any on https://tunit.dev/docs
    Or maybe my search skills are lacking, but exception and ThrowsAsync don't find anything.
    Because I figured my brain was fooling me at first, but only when I started digging, I saw the behavior I expected/thought was there all along.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions