|
| 1 | +namespace MrKWatkins.Assertions.Tests; |
| 2 | + |
| 3 | +public sealed class AsyncActionAssertionsTests |
| 4 | +{ |
| 5 | + [Test] |
| 6 | + public async Task ThrowAsync() |
| 7 | + { |
| 8 | + Func<Task> doesNotThrow = () => Task.CompletedTask; |
| 9 | + |
| 10 | + var exception = new InvalidOperationException("Test"); |
| 11 | + Func<Task> throws = () => throw exception; |
| 12 | + |
| 13 | + Func<Task> throwsWrongException = () => throw new NotSupportedException("Wrong"); |
| 14 | + |
| 15 | + await Assert.That(() => throws.Should().ThrowAsync<InvalidOperationException>()).ThrowsNothing(); |
| 16 | + |
| 17 | + await Assert.That(() => doesNotThrow.Should().ThrowAsync<InvalidOperationException>()) |
| 18 | + .Throws<AssertionException>().WithMessage("Function should throw an InvalidOperationException."); |
| 19 | + |
| 20 | + await Assert.That(() => throwsWrongException.Should().ThrowAsync<InvalidOperationException>()) |
| 21 | + .Throws<AssertionException>().WithMessage("Function should throw an InvalidOperationException but threw a NotSupportedException with message \"Wrong\"."); |
| 22 | + } |
| 23 | + |
| 24 | + [Test] |
| 25 | + public async Task ThrowAsync_Chain() |
| 26 | + { |
| 27 | + var exception = new InvalidOperationException("Test"); |
| 28 | + Func<Task> throws = () => throw exception; |
| 29 | + |
| 30 | + var chain = await throws.Should().ThrowAsync<InvalidOperationException>().ConfigureAwait(false); |
| 31 | + |
| 32 | + await Assert.That(chain.Exception).IsSameReferenceAs(exception); |
| 33 | + await Assert.That(chain.That).IsSameReferenceAs(exception); |
| 34 | + } |
| 35 | + |
| 36 | + [Test] |
| 37 | + public async Task ThrowAsync_String() |
| 38 | + { |
| 39 | + Func<Task> doesNotThrow = () => Task.CompletedTask; |
| 40 | + |
| 41 | + var exception = new InvalidOperationException("Test"); |
| 42 | + Func<Task> throws = () => throw exception; |
| 43 | + |
| 44 | + Func<Task> throwsWrongException = () => throw new NotSupportedException("Wrong"); |
| 45 | + |
| 46 | + await Assert.That(() => throws.Should().ThrowAsync<InvalidOperationException>("Test")).ThrowsNothing(); |
| 47 | + |
| 48 | + await Assert.That(() => throws.Should().ThrowAsync<InvalidOperationException>("Wrong Message")) |
| 49 | + .Throws<AssertionException>().WithMessage("Value should have Message \"Wrong Message\" but was \"Test\"."); |
| 50 | + |
| 51 | + await Assert.That(() => doesNotThrow.Should().ThrowAsync<InvalidOperationException>("Test")) |
| 52 | + .Throws<AssertionException>().WithMessage("Function should throw an InvalidOperationException."); |
| 53 | + |
| 54 | + await Assert.That(() => throwsWrongException.Should().ThrowAsync<InvalidOperationException>("Test")) |
| 55 | + .Throws<AssertionException>().WithMessage("Function should throw an InvalidOperationException but threw a NotSupportedException with message \"Wrong\"."); |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + public async Task ThrowAsync_String_Chain() |
| 60 | + { |
| 61 | + var exception = new InvalidOperationException("Test"); |
| 62 | + Func<Task> throws = () => throw exception; |
| 63 | + |
| 64 | + var chain = await throws.Should().ThrowAsync<InvalidOperationException>("Test").ConfigureAwait(false); |
| 65 | + |
| 66 | + await Assert.That(chain.Exception).IsSameReferenceAs(exception); |
| 67 | + await Assert.That(chain.That).IsSameReferenceAs(exception); |
| 68 | + } |
| 69 | + |
| 70 | + [Test] |
| 71 | + public async Task NotThrowAsync() |
| 72 | + { |
| 73 | + Func<Task> doesNotThrow = () => Task.CompletedTask; |
| 74 | + |
| 75 | + var exception = new InvalidOperationException("Test"); |
| 76 | + Func<Task> throws = () => throw exception; |
| 77 | + |
| 78 | + await Assert.That(() => doesNotThrow.Should().NotThrowAsync()).ThrowsNothing(); |
| 79 | + |
| 80 | + var actualException = await Assert.That(() => throws.Should().NotThrowAsync()) |
| 81 | + .Throws<AssertionException>() |
| 82 | + .WithMessage("Function should not throw but threw an InvalidOperationException with message \"Test\"."); |
| 83 | + await Assert.That(actualException!.InnerException).IsSameReferenceAs(exception); |
| 84 | + } |
| 85 | + |
| 86 | + [Test] |
| 87 | + public async Task ThrowAsync_ActuallyAsync() |
| 88 | + { |
| 89 | + var exception = new InvalidOperationException("AsyncTest"); |
| 90 | + Func<Task> throwsAsync = async () => |
| 91 | + { |
| 92 | + await Task.Yield(); |
| 93 | + throw exception; |
| 94 | + }; |
| 95 | + |
| 96 | + var chain = await throwsAsync.Should().ThrowAsync<InvalidOperationException>("AsyncTest").ConfigureAwait(false); |
| 97 | + await Assert.That(chain.Exception).IsSameReferenceAs(exception); |
| 98 | + } |
| 99 | + |
| 100 | + [Test] |
| 101 | + public async Task NotThrowAsync_ActuallyAsync() |
| 102 | + { |
| 103 | + Func<Task> doesNotThrow = async () => await Task.Yield(); |
| 104 | + |
| 105 | + await Assert.That(() => doesNotThrow.Should().NotThrowAsync()).ThrowsNothing(); |
| 106 | + } |
| 107 | + |
| 108 | + [Test] |
| 109 | + public async Task Awaiting_InvokingExtensions() |
| 110 | + { |
| 111 | + var value = new TestClass(); |
| 112 | + |
| 113 | + await Assert.That(() => value.Awaiting(v => v.ThrowAsync()).Should().ThrowAsync<InvalidOperationException>()) |
| 114 | + .ThrowsNothing(); |
| 115 | + |
| 116 | + await Assert.That(() => value.Awaiting(v => v.NotThrowAsync()).Should().NotThrowAsync()) |
| 117 | + .ThrowsNothing(); |
| 118 | + } |
| 119 | + |
| 120 | + [Test] |
| 121 | + public async Task Awaiting_InvokingExtensions_WithReturn() |
| 122 | + { |
| 123 | + var value = new TestClass(); |
| 124 | + |
| 125 | + await Assert.That(() => value.Awaiting(v => v.ThrowWithReturnAsync()).Should().ThrowAsync<InvalidOperationException>()) |
| 126 | + .ThrowsNothing(); |
| 127 | + } |
| 128 | + |
| 129 | + private sealed class TestClass |
| 130 | + { |
| 131 | + public Task ThrowAsync() => throw new InvalidOperationException(); |
| 132 | + |
| 133 | + public async Task NotThrowAsync() => await Task.Yield(); |
| 134 | + |
| 135 | + public Task<string> ThrowWithReturnAsync() => throw new InvalidOperationException(); |
| 136 | + } |
| 137 | +} |
0 commit comments