Hi.
We have a lot of code similar to this:
public MyClass(IEventStoreConnection eventStoreConnection)
{
EventStoreConnection = eventStoreConnection;
}
private readonly IEventStoreConnection EventStoreConnection;
private EventStoreStreamCatchUpSubscription Subscription;
public void StartSubscription()
{
var eventReadResult = EventStoreConnection.ReadEventAsync(“some_stream_snapshots”, -1, true).Result;
if (eventReadResult.Status == EventReadStatus.Success && eventReadResult.Event != null)
{
var snapshotEventData = eventReadResult.Event.Value;
var snapshot = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(snapshotEventData.Event.Data));
// Do things with snapshot
}
Subscription = EventStoreConnection.SubscribeToStreamFrom("$some_stream", latestSnapshottedEventSequenceNumber, true, OnEventAppeared, OnLiveProcessingStarted, OnSubscriptionDropped);
}
which we would like to unit test. In particular we would like to unit test our error handling (when eventReadResult.Status != EventReadStatus.Success) and the deserialization of the eventData.Event.Data.
Typically we would mock IEventStoreConnection and its ReadEventAsync method, injecting the results we want to test. However we cannot take this approach with GetEventStore because most (if not all) of the classes involved have an internal constructor, which prevents us from creating instances:
[Test]
public void Should_throw_if_cant_read_snapshots()
{
var eventStoreConnection = new Mock();
eventStoreConnection
.Setup(x => x.ReadEventAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()))
.Returns(() => Task.FromResult(new EventReadResult(/* Values we want to inject but we can’t because the constructor is internal*/)));
}
This is just an example and we’re facing the same issue with ResolvedEvent and RecordedEvent (to test the processing of events),
The only alternative we can think of is to create our own abstractions (and the corresponding Adapters) for IEventStoreConnection and all the other classes mentioned above, but that will force us to have more code to maintain and to create one or more additional objects for processing each event due to the required (and unnecessary?) mapping, putting more pressure on the GC.
I’d like to hear what’s the recommended approach and what are other people doing in order to unit test their code when it has dependencies on GetEventStore classes.
Thanks,
Rodolfo