我有一个使用 Jonathan Oliver 的CommonDomain和EventStore的小系统。
如何对聚合根进行单元测试以验证是否引发了正确的事件?
考虑以下聚合根:
public class Subscriber : AggregateBase
{
private Subscriber(Guid id)
{
this.Id = id;
}
private Subscriber(Guid id, string email, DateTimeOffset registeredDate)
: this(id)
{
this.RaiseEvent(new NewSubscriberRegistered(this.Id, email, registeredDate));
}
public string Email{ get; private set; }
public DateTimeOffset RegisteredDate { get; private set; }
public static Subscriber Create(Guid id, string email, DateTimeOffset registeredDate)
{
return new Subscriber(id, email, registeredDate);
}
private void Apply(NewSubscriberRegistered @event)
{
this.Email = @event.Email;
this.RegisteredDate = @event.RegisteredDate;
}
}
我想写一个以下测试:
// Arrange
var id = Guid.NewGuid();
var email = "test@thelightfull.com";
var registeredDate = DateTimeOffset.Now;
// Act
var subscriber = Subscriber.Create(id, email, registeredDate);
// Assert
var eventsRaised = subscriber.GetEvents(); <---- How to get the events?
// Assert that NewSubscriberRegistered event was raised with valid data
我可以使用内存持久性和同步调度程序设置整个 EventStore,连接模拟事件处理程序并存储任何已发布的事件以进行验证,但这似乎有点矫枉过正。
CommonDomain中有一个接口IRouteEvents
。看起来我可以模拟它以直接从中获取事件,AggregateBase
但我实际上如何将它传递给我的Subscriber
班级?我不想用与测试相关的代码“污染”我的领域。