Reputation: 10221
I am working on a new application using Microsoft Orleans and .NET 8, and I need to add some tests. I am currently experimenting with Microsoft.Orleans.TestingHost
following Unit testing with Orleans
In preparation for the tests, I have created the following fixture:
public sealed class ClusterFixture : IDisposable
{
public TestCluster Cluster { get; } = new TestClusterBuilder().Build();
public ClusterFixture() => Cluster.Deploy();
void IDisposable.Dispose() => Cluster.StopAllSilos();
}
I defined also a collection for my tests:
[CollectionDefinition(Name)]
public sealed class ClusterCollection : ICollectionFixture<ClusterFixture>
{
public const string Name = nameof(ClusterCollection);
}
My first test looks like this:
[Collection(ClusterCollection.Name)]
public class ClusterTests(ClusterFixture fixture)
{
private readonly TestCluster _cluster = fixture.Cluster;
[Fact]
public async Task Game_Name_Should_Be_Invernomuto()
{
var game = _cluster.GrainFactory.GetGrain<IGameGrain>(1);
var gameInfo = await game.GetState();
Assert.Equal(CONSTANTS.CLUSTER.GAME_NAME, gameInfo.Name);
}
}
However, I can't get the test to work:
System.ArgumentException: Could not find an implementation for interface InvernomutoGrainInterfaces.IGameGrain
at Orleans.GrainInterfaceTypeToGrainTypeResolver.GetGrainType(GrainInterfaceType interfaceType) in /_/src/Orleans.Core/Core/GrainInterfaceTypeToGrainTypeResolver.cs:line 102
What am I doing wrong?
Upvotes: 1
Views: 346
Reputation: 10221
The main problem was the missing project reference to the grains implementation in the test project...
Then I discovered that the cluster fixture was missing a proper storage configurator:
public class SiloMemoryStorageConfigurator : ISiloConfigurator
{
public void Configure(ISiloBuilder hostBuilder)
{
hostBuilder.AddMemoryGrainStorage(CONSTANTS.CLUSTER.INVERNOMUTO);
hostBuilder.AddMemoryGrainStorageAsDefault();
hostBuilder.AddReminders();
hostBuilder.UseInMemoryReminderService();
}
}
when the configurator has been added:
public sealed class ClusterFixture : IDisposable
{
public TestCluster Cluster { get; } = new TestClusterBuilder
{
Options = { ServiceId = Guid.NewGuid().ToString() }
}
.AddSiloBuilderConfigurator<SiloMemoryStorageConfigurator>()
.Build();
public ClusterFixture() => Cluster.Deploy();
void IDisposable.Dispose() => Cluster.StopAllSilos();
}
the test works
Upvotes: 1