Skip to content
Merged
80 changes: 80 additions & 0 deletions TUnit.Assertions.Tests/Assertions/Delegates/Throws.ExactlyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
namespace TUnit.Assertions.Tests.Assertions.Delegates;

public partial class Throws
{
public class ExactlyTests
{
[Test]
public async Task Fails_For_Code_With_Other_Exceptions()
{
string expectedMessage = """
Expected action to throw exactly a CustomException, but an OtherException was thrown.
At Assert.That(action).ThrowsExactly<CustomException>()
""";
Exception exception = CreateOtherException();
Action action = () => throw exception;

var sut = async ()
=> await Assert.That(action).ThrowsExactly<CustomException>();

await Assert.That(sut).ThrowsException()
.WithMessage(expectedMessage);
}

[Test]
public async Task Fails_For_Code_With_Subtype_Exceptions()
{
string expectedMessage = """
Expected action to throw exactly a CustomException, but a SubCustomException was thrown.
At Assert.That(action).ThrowsExactly<CustomException>()
""";
Exception exception = CreateSubCustomException();
Action action = () => throw exception;

var sut = async ()
=> await Assert.That(action).ThrowsExactly<CustomException>();

await Assert.That(sut).ThrowsException()
.WithMessage(expectedMessage);
}

[Test]
public async Task Fails_For_Code_Without_Exceptions()
{
string expectedMessage = """
Expected action to throw exactly a CustomException, but none was thrown.
At Assert.That(action).ThrowsExactly<CustomException>()
""";
Action action = () => { };

var sut = async ()
=> await Assert.That(action).ThrowsExactly<CustomException>();

await Assert.That(sut).ThrowsException()
.WithMessage(expectedMessage);
}

[Test]
public async Task Returns_Exception_When_Awaited()
{
Exception exception = CreateCustomException();
Action action = () => throw exception;

var result = await Assert.That(action).ThrowsExactly<CustomException>();

await Assert.That((object?)result).IsSameReference(exception);
}

[Test]
public async Task Succeeds_For_Code_With_Correct_Exception()
{
Exception exception = CreateCustomException();
Action action = () => throw exception;

var sut = async ()
=> await Assert.That(action).ThrowsExactly<CustomException>();

await Assert.That(sut).ThrowsNothing();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace TUnit.Assertions.Tests.Assertions.Delegates;

public partial class Throws
{
public class ExceptionTests
{
[Test]
public async Task Fails_For_Code_Without_Exceptions()
{
string expectedMessage = """
Expected action to throw an exception, but none was thrown.
At Assert.That(action).ThrowsException()
""";
Action action = () => { };

var sut = async ()
=> await Assert.That(action).ThrowsException();

await Assert.That(sut).ThrowsException()
.WithMessage(expectedMessage);
}

[Test]
public async Task Returns_Exception_When_Awaited()
{
Exception exception = CreateCustomException();
Action action = () => throw exception;

var result = await Assert.That(action).ThrowsException();

await Assert.That((object?)result).IsSameReference(exception);
}

[Test]
public async Task Succeeds_For_Code_With_Exceptions()
{
Exception exception = CreateCustomException();
Action action = () => throw exception;

var sut = async ()
=> await Assert.That(action).ThrowsException();

await Assert.That(sut).ThrowsNothing();
}
}
}
47 changes: 47 additions & 0 deletions TUnit.Assertions.Tests/Assertions/Delegates/Throws.NothingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace TUnit.Assertions.Tests.Assertions.Delegates;

public partial class Throws
{
public class NothingTests
{
[Test]
public async Task Fails_For_Code_With_Exceptions()
{
string expectedMessage = $$"""
Expected action to throw nothing, but a CustomException was thrown:
{{nameof(Fails_For_Code_With_Exceptions)}}.
At Assert.That(action).ThrowsNothing()
""";
Exception exception = CreateCustomException();
Action action = () => throw exception;

var sut = async ()
=> await Assert.That(action).ThrowsNothing();

await Assert.That(sut).ThrowsException()
.WithMessage(expectedMessage);
}

[Test]
public async Task Succeeds_For_Code_Without_Exceptions()
{
Action action = () => { };

var sut = async ()
=> await Assert.That(action).ThrowsNothing();

await Assert.That(sut).ThrowsNothing();
}

[Test]
public async Task Returns_Awaited_Result()
{
int value = 42;
Func<int> action = () => value;

var result = await Assert.That(action).ThrowsNothing();

await Assert.That(result).IsEqualTo(value);
}
}
}
92 changes: 92 additions & 0 deletions TUnit.Assertions.Tests/Assertions/Delegates/Throws.OfTypeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
namespace TUnit.Assertions.Tests.Assertions.Delegates;

public partial class Throws
{
public class OfTypeTests
{
[Test]
public async Task Fails_For_Code_With_Other_Exceptions()
{
string expectedMessage = """
Expected action to throw a CustomException, but an OtherException was thrown.
At Assert.That(action).Throws<CustomException>()
""";
Exception exception = CreateOtherException();
Action action = () => throw exception;

var sut = async ()
=> await Assert.That(action).Throws<CustomException>();

await Assert.That(sut).ThrowsException()
.WithMessage(expectedMessage);
}

[Test]
public async Task Fails_For_Code_With_Supertype_Exceptions()
{
string expectedMessage = """
Expected action to throw a SubCustomException, but a CustomException was thrown.
At Assert.That(action).Throws<SubCustomException>()
""";
Exception exception = CreateCustomException();
Action action = () => throw exception;

var sut = async ()
=> await Assert.That(action).Throws<SubCustomException>();

await Assert.That(sut).ThrowsException()
.WithMessage(expectedMessage);
}

[Test]
public async Task Fails_For_Code_Without_Exceptions()
{
string expectedMessage = """
Expected action to throw a CustomException, but none was thrown.
At Assert.That(action).Throws<CustomException>()
""";
Action action = () => { };

var sut = async ()
=> await Assert.That(action).Throws<CustomException>();

await Assert.That(sut).ThrowsException()
.WithMessage(expectedMessage);
}

[Test]
public async Task Returns_Exception_When_Awaited()
{
Exception exception = CreateCustomException();
Action action = () => throw exception;

var result = await Assert.That(action).Throws<CustomException>();

await Assert.That((object?)result).IsSameReference(exception);
}

[Test]
public async Task Succeeds_For_Code_With_Subtype_Exceptions()
{
Exception exception = CreateSubCustomException();
Action action = () => throw exception;

var sut = async ()
=> await Assert.That(action).Throws<CustomException>();

await Assert.That(sut).ThrowsNothing();
}

[Test]
public async Task Succeeds_For_Code_With_Exact_Exceptions()
{
Exception exception = CreateCustomException();
Action action = () => throw exception;

var sut = async ()
=> await Assert.That(action).Throws<CustomException>();

await Assert.That(sut).ThrowsNothing();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
namespace TUnit.Assertions.Tests.Assertions.Delegates;

public partial class Throws
{
public class WithInnerExceptionTests
{
[Test]
public async Task Fails_For_Different_Messages_In_Inner_Exception()
{
string outerMessage = "foo";
string expectedInnerMessage = "bar";
string expectedMessage = """
Expected action to throw an Exception which message equals "bar", but it differs at index 0:
"some different inner message"
"bar"
↑.
At Assert.That(action).ThrowsException().WithInnerException().WithMessage(expectedInnerMessage)
""";
Exception exception = CreateCustomException(outerMessage,
CreateCustomException("some different inner message"));
Action action = () => throw exception;

var sut = async ()
=> await Assert.That(action).ThrowsException()
.WithInnerException().WithMessage(expectedInnerMessage);

await Assert.That(sut).ThrowsException()
.WithMessage(expectedMessage);
}

[Test]
public async Task Returns_Exception_When_Awaited()
{
Exception exception = CreateCustomException(
innerException: CreateCustomException());
Action action = () => throw exception;

var result = await Assert.That(action).Throws<CustomException>().WithInnerException();

await Assert.That((object?)result).IsSameReference(exception);
}

[Test]
public async Task Succeed_For_Matching_Message()
{
string outerMessage = "foo";
string innerMessage = "bar";
Exception exception = CreateCustomException(outerMessage, CreateCustomException(innerMessage));
Action action = () => throw exception;

var sut = async ()
=> await Assert.That(action).ThrowsException()
.WithInnerException().WithMessage(innerMessage);

await Assert.That(sut).ThrowsNothing();
}
}
}
Loading