I was stuck on this for quite a while, but went looking through Fluxor's glitter community and found something that might help! I understand that you'd like to test using Moq, but perhaps what I found might be easier/suit your purposes for testing Fluxor components!
Instead of using Moq, I followed this format and managed to get something working:
Test.cs
public class InhousePetOwnerStateTests
{
private readonly IServiceProvider ServiceProvider;
private readonly IStore Store;
private readonly IState<InhousePetOwnerListState> State;
public InhousePetOwnerStateTests()
{
var services = new ServiceCollection();
services.AddFluxor(x =>
x
.ScanAssemblies(GetType().Assembly)
.ScanTypes(typeof(InhousePetOwnerListState), typeof(Reducers)));
ServiceProvider = services.BuildServiceProvider();
Store = ServiceProvider.GetRequiredService<IStore>();
State = ServiceProvider.GetRequiredService<IState<InhousePetOwnerListState>>();
Store.InitializeAsync().Wait();
}
[Fact]
public void SetInhousePetOwnerList_Empty_StateUpdated()
{
Store.Dispatch(new SetInhousePetOwnerListAction(new()));
Assert.Empty(State.Value.InhousePetOwners);
}
[Fact]
public void SetInhousePetOwnerList_MultipleInhouseOwners_StateUpdated()
{
List<InhousePetOwner> inhousePetOwners = new List<InhousePetOwner>
{
new InhousePetOwner()
{
InhousePetOwnerId = 1,
PetOwnerId = 1,
TimeIn = System.DateTime.Now
},
new InhousePetOwner()
{
InhousePetOwnerId = 2,
PetOwnerId = 2,
TimeIn = System.DateTime.Now
},
};
Store.Dispatch(new SetInhousePetOwnerListAction(inhousePetOwners));
Assert.Equal(2, State.Value.InhousePetOwners.Count);
}
}
In the constructor, we create a service collection and add Fluxor (just like you would to your Startup.cs), then scan for assemblies.
In my code above, I scanned for the Reducers and the State. Please refer to my directory structure here (you'll get an idea of how I decided what to include).
Actions.cs
public class SetInhousePetOwnerListAction
{
public List<InhousePetOwner> InhousePetOwners { get; }
public SetInhousePetOwnerListAction(List<InhousePetOwner> inhousePetOwners)
{
InhousePetOwners = inhousePetOwners;
}
}
Reducers.cs (scanned in the constructor)
public static class Reducers
{
[ReducerMethod]
public static InhousePetOwnerListState ReduceSetInhousePetOwnerListAction(InhousePetOwnerListState state, SetInhousePetOwnerListAction action)
{
return new InhousePetOwnerListState(inhousePetOwners: action.InhousePetOwners);
}
}
InhousePetOwnerState.cs (scanned in the constructor)
public class InhousePetOwnerListState
{
public List<InhousePetOwner> InhousePetOwners { get; }
private InhousePetOwnerListState() { }
public InhousePetOwnerListState(List<InhousePetOwner> inhousePetOwners)
{
InhousePetOwners = inhousePetOwners;
}
}
What this solution does is it allows you to inject all your Fluxor states, actions and reducers, enabling you to call them as you normally would - I've found it pretty easy to test this way!
mock.Setup(foo => foo.Name).Returns("bar");
– Fundament