-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyAsyncServiceTests.cs
44 lines (36 loc) · 1.05 KB
/
MyAsyncServiceTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using Moq;
using NUnit.Framework;
using System.Threading;
using System.Threading.Tasks;
[TestFixture]
public class MyAsyncServiceTests
{
private MyAsyncService _service;
[SetUp]
public void Setup()
{
_service = new MyAsyncService();
}
[Test]
public async Task MyAsyncMethod_CompletesSuccessfully_WhenNotCancelled()
{
// Arrange
var cancellationTokenSource = new CancellationTokenSource();
var token = cancellationTokenSource.Token;
// Act
var result = await _service.MyAsyncMethod(token);
// Assert
Assert.IsTrue(result);
}
[Test]
public void MyAsyncMethod_ThrowsOperationCanceledException_WhenCancelled()
{
// Arrange
var cancellationTokenSource = new CancellationTokenSource();
var token = cancellationTokenSource.Token;
// Cancel the token
cancellationTokenSource.Cancel();
// Act & Assert
Assert.ThrowsAsync<OperationCanceledException>(async () => await _service.MyAsyncMethod(token));
}
}