1
#if NET_3_5 || NET_4_0 || NET_4_5
3
using System.Threading.Tasks;
5
using NUnit.Framework.Constraints;
7
namespace nunit.core.tests.net45
10
public class ThrowsTests
13
public void ThrowsConstraintAsyncTask()
15
Assert.IsTrue(new ThrowsConstraint(new ExactTypeConstraint(typeof(InvalidOperationException)))
16
.Matches(new TestDelegate(async () => await ThrowAsyncNonGenericTask())));
20
public void ThrowsConstraintAsyncGenericTask()
22
Assert.IsTrue(new ThrowsConstraint(new ExactTypeConstraint(typeof(InvalidOperationException)))
23
.Matches(new TestDelegate(async () => { await ThrowAsyncGenericTask(); })));
27
public void ThrowsConstraintAsyncTaskRunSynchronously()
29
#pragma warning disable 1998
30
Assert.IsTrue(new ThrowsConstraint(new ExactTypeConstraint(typeof(InvalidOperationException)))
31
.Matches(new TestDelegate(async () => { throw new InvalidOperationException(); })));
32
#pragma warning restore 1998
36
public void AssertThrows()
38
Assert.Throws(typeof(InvalidOperationException), async () => await ThrowAsyncNonGenericTask());
42
public void AssertThrowsGenericTask()
44
Assert.Throws(typeof(InvalidOperationException), async () => await ThrowAsyncGenericTask());
48
public void AssertThat()
50
Assert.That(async () => await ThrowAsyncNonGenericTask(), Throws.TypeOf<InvalidOperationException>());
54
public void AssertThatGenericTask()
56
Assert.That(async () => await ThrowAsyncGenericTask(), Throws.TypeOf<InvalidOperationException>());
60
public void AssertThrowsGeneric()
62
Assert.Throws<InvalidOperationException>(async () => await ThrowAsyncNonGenericTask());
66
public void AssertThrowsGenericWithGenericTask()
68
Assert.Throws<InvalidOperationException>(async () => await ThrowAsyncGenericTask());
71
private static async Task ThrowAsyncNonGenericTask()
73
await Task.Run(() => 1);
74
throw new InvalidOperationException();
77
private static async Task<int> ThrowAsyncGenericTask()
79
await ThrowAsyncNonGenericTask();
80
return await Task.Run(() => 1);
b'\\ No newline at end of file'