mrpmorris / Fluxor

Fluxor is a zero boilerplate Flux/Redux library for Microsoft .NET and Blazor.
MIT License
1.24k stars 141 forks source link

Async effect difficult to unit test #277

Closed ABIOLI-COM closed 2 years ago

ABIOLI-COM commented 2 years ago

If I have an effect like the following:

    public class FluxorEffects
    {
        [EffectMethod]
        public async Task OnExecuteRequestAction(ExecuteRequestAction action, IDispatcher dispatcher)
        {
            var result = await <something>;
            dispatcher.Dispatch(new SetResultsAction(result));
        }
    }

And I want to write a unit test for this, I end up with something like:

  dispatcher.Dispatch(new ExecutRequestAction());
  Assert.Equal("A", someState.value);

Of course, not awaiting anywhere, the Assert is executed way before the effect. I don't really understand how I can dispatch synchronously an action that is then processed (in the effect) asynchronously... I'm missing something, surely... :-) Thanks!

mrpmorris commented 2 years ago

Hi

To unit test, use a mocking framework like Moq.

Create an instance of your effect class, then call your effect method, passing in a mocked IDispatcher.