Skip to content

Instantly share code, notes, and snippets.

@dcomartin

dcomartin/AggregateTests.cs Secret

Created May 20, 2021
Embed
What would you like to do?
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