You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Documentation on writing unit tests, specifically for Actors accessing state would be great.
The below code, for instance, would throw: "InvalidOperationException: The actor was initialized without a state provider, and so cannot interact with state. If this is inside a unit test, replace Actor.StateProvider with a mock."
internal class MyActor : Actor, IMyActor
{
private const string STATE_KEY = "counter";
public MyActor(ActorHost host) // Accept ActorHost in the constructor
: base(host) // Pass ActorHost to the base class constructor
{
}
public async Task<int> GetCounter()
{
var iCounterState =(await StateManager.TryGetStateAsync<int>(STATE_KEY).ConfigureAwait(false));
return iCounterState.Value;
}
public async Task Increment()
{
var iCounterState = (await StateManager.TryGetStateAsync<int>(STATE_KEY).ConfigureAwait(false));
if (iCounterState.HasValue)
{
await StateManager.SetStateAsync(STATE_KEY, iCounterState.Value + 1).ConfigureAwait(false);
}
else
{
await StateManager.SetStateAsync(STATE_KEY, 1).ConfigureAwait(false);
}
}
}
public class MyActorTests
{
[Fact]
public async Task SomeMethod_ShouldSucceed()
{
// Arrange
var actorId = new ActorId("test");
var host = ActorHost.CreateForTest<MyActor>(new ActorTestOptions
{
ActorId = actorId
});
var myActor = new MyActor(host);
// Act
await myActor.Increment();
var counter = myActor.GetCounter();
// Assert
Assert.AreEqual(1, counter);
}
}
The text was updated successfully, but these errors were encountered:
jaq316
changed the title
Document how to write unit test for Actors
Document how to write unit test for Actors that access state
Oct 17, 2023
jaq316
changed the title
Document how to write unit test for Actors that access state
Documentation on writing unit tests for Actors that access state
Oct 17, 2023
@jaq316 is this a recommendation to provide an IActorStartManager parameter in every actor, so it is testable?
That sounds kinda strange because in the real world the Actors doesn't need this injecten
Documentation on writing unit tests, specifically for Actors accessing state would be great.
The below code, for instance, would throw: "InvalidOperationException: The actor was initialized without a state provider, and so cannot interact with state. If this is inside a unit test, replace Actor.StateProvider with a mock."
The text was updated successfully, but these errors were encountered: