Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created May 20, 2021 21:09
Show Gist options
  • Save dcomartin/ca55b79eea0be0ccde07b5894094ba63 to your computer and use it in GitHub Desktop.
Save dcomartin/ca55b79eea0be0ccde07b5894094ba63 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using Shouldly;
namespace EventSourcing.Demo
{
public abstract class AggregateTests<TAggregate> where TAggregate : AggregateRoot
{
private readonly TAggregate _aggregateRoot;
protected AggregateTests(TAggregate aggregateRoot)
{
_aggregateRoot = aggregateRoot;
}
protected void Given(params IEvent[] events)
{
if (events != null)
{
_aggregateRoot.Load(events);
}
}
protected void When(Action<TAggregate> command)
{
command(_aggregateRoot);
}
protected void Then<TEvent>(params Action<TEvent>[] conditions)
{
var events = _aggregateRoot.GetUncommittedEvents();
events.Count.ShouldBe(1);
var evnt = events.First();
evnt.ShouldBeOfType<TEvent>();
if (conditions != null)
{
((TEvent)evnt).ShouldSatisfyAllConditions(conditions);
}
}
protected void Throws<TException>(Action<TAggregate> command, params Action<TException>[] conditions) where TException : Exception
{
var ex = Should.Throw<TException>(() => command(_aggregateRoot));
if (conditions != null)
{
ex.ShouldSatisfyAllConditions(conditions);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment