1
+ using Dapr . Actors . Runtime ;
2
+ using DaprDemoActor ;
3
+ using IDemoActorInterface ;
4
+ using Moq ;
5
+
1
6
namespace DemoActor . UnitTest
2
7
{
3
- public class UnitTest1
8
+ public class DemoActorTests
4
9
{
5
10
[ Fact ]
6
- public void Test1 ( )
11
+ public async Task SaveData_CorrectlyPersistDataWithGiveTTL ( )
12
+ {
13
+ // arrange
14
+ // Name of the state to be saved in the actor
15
+ var actorStateName = "my_data" ;
16
+ // Create a mock actor state manager to simulate the actor state
17
+ var mockStateManager = new Mock < IActorStateManager > ( MockBehavior . Strict ) ;
18
+ // Prepare other dependencies
19
+ var bankService = new BankService ( ) ;
20
+ // Create an actor host for testing
21
+ var host = ActorHost . CreateForTest < DaprDemoActor . DemoActor > ( ) ;
22
+ // Create an actor instance with the mock state manager and its dependencies
23
+ var storageActor = new DaprDemoActor . DemoActor ( host , bankService , mockStateManager . Object ) ;
24
+ // Prepare test data to be saved
25
+ var data = new MyDataWithTTL
26
+ {
27
+ MyData = new MyData
28
+ {
29
+ PropertyA = "PropA" ,
30
+ PropertyB = "PropB" ,
31
+ } ,
32
+ TTL = TimeSpan . FromSeconds ( 10 )
33
+ } ;
34
+ // Setup the mock state manager to enable the actor to save the state with the SetStateAsync method, and return
35
+ // a completed task when the state is saved, so that the actor can continue with the test.
36
+ // When MockBehavior.Strict is used, the test will fail if the actor does not call SetStateAsync or
37
+ // calls other methods on the state manager.
38
+ mockStateManager
39
+ . Setup ( x => x . SetStateAsync ( It . IsAny < string > ( ) , It . IsAny < MyData > ( ) , It . IsAny < TimeSpan > ( ) , It . IsAny < CancellationToken > ( ) ) )
40
+ . Returns ( Task . CompletedTask ) ;
41
+
42
+ // act
43
+ await storageActor . SaveData ( data ) ;
44
+
45
+ // assert
46
+ // Verify that the state manager is called with the correct state name and data, only one time.
47
+ mockStateManager . Verify ( x => x . SetStateAsync (
48
+ actorStateName ,
49
+ It . Is < MyData > ( x => x . PropertyA == "PropA" && x . PropertyB == "PropB" ) ,
50
+ It . Is < TimeSpan > ( x => x . TotalSeconds == 10 ) ,
51
+ It . IsAny < CancellationToken > ( ) ) ,
52
+ Times . Once ) ;
53
+ }
54
+
55
+ [ Fact ]
56
+ public async Task GetData_CorrectlyRetrieveData ( )
7
57
{
58
+ // arrange
59
+ // Name of the state to be saved in the actor
60
+ var actorStateName = "my_data" ;
61
+ // Create a mock actor state manager to simulate the actor state
62
+ var mockStateManager = new Mock < IActorStateManager > ( MockBehavior . Strict ) ;
63
+ // Prepare other dependencies
64
+ var bankService = new BankService ( ) ;
65
+ // Create an actor host for testing
66
+ var host = ActorHost . CreateForTest < DaprDemoActor . DemoActor > ( ) ;
67
+ // Create an actor instance with the mock state manager and its dependencies
68
+ var storageActor = new DaprDemoActor . DemoActor ( host , bankService , mockStateManager . Object ) ;
69
+ // Prepare prepare the state to be returned by the state manager
70
+ var state = new MyData
71
+ {
72
+ PropertyA = "PropA" ,
73
+ PropertyB = "PropB" ,
74
+ } ;
75
+ // Setup the mock state manager to return the state when the actor calls GetStateAsync.
76
+ mockStateManager
77
+ . Setup ( x => x . GetStateAsync < MyData > ( actorStateName , It . IsAny < CancellationToken > ( ) ) )
78
+ . Returns ( Task . FromResult ( state ) ) ;
79
+
80
+ // act
81
+ var result = await storageActor . GetData ( ) ;
8
82
83
+ // assert
84
+ // Verify that the state manager is called with the correct state name, only one time.
85
+ mockStateManager . Verify ( x => x . GetStateAsync < MyData > ( actorStateName , It . IsAny < CancellationToken > ( ) ) , Times . Once ) ;
86
+ // Verify that the actor returns the correct data.
87
+ Assert . Equal ( "PropA" , result . PropertyA ) ;
88
+ Assert . Equal ( "PropB" , result . PropertyB ) ;
9
89
}
10
90
}
11
- }
91
+ }
0 commit comments