I can\'t test a Reliable Service/Actor by just calling it\'s constructor and then test it\'s methods. var testService = new SomeService();
throws a NullReferenceExc
For mocking the state manager in Reliable Actors, you can do something like this:
private readonly IActorStateManager _stateManager;
public MyActor(IActorStateManager stateManager = null)
{
_stateManager = stateManager ?? this.StateManager;
}
Actually, the StateManager
isn't yet initialized at this time. We can get it when OnActivateAsync
is called:
private IActorStateManager _stateManager;
// Unit tests can inject mock here.
public MyActor(IActorStateManager stateManager = null)
{
_stateManager = stateManager;
}
protected override async Task OnActivateAsync()
{
if (_stateManager == null)
{
_stateManager = StateManager;
}
}
Just make sure to always use _stateManager
in the rest of the code instead of this.StateManager
.