From 9e2e2c0a640683592a29732da3730a0cb5a0d0de Mon Sep 17 00:00:00 2001 From: tumiwisista Date: Sun, 8 Jun 2025 02:27:42 +0200 Subject: [PATCH] Add read-only domain events test --- .../AggregateKit.Tests/AggregateRootTests.cs | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/AggregateKit.Tests/AggregateRootTests.cs b/tests/AggregateKit.Tests/AggregateRootTests.cs index 23d123e..05bf80f 100644 --- a/tests/AggregateKit.Tests/AggregateRootTests.cs +++ b/tests/AggregateKit.Tests/AggregateRootTests.cs @@ -32,6 +32,18 @@ public ArticlePublishedEvent(Guid articleId, DateTime publishDate) } } + private class TestEvent : DomainEventBase + { + } + + private class TestAggregate : AggregateRoot + { + public TestAggregate(Guid id) : base(id) + { + AddDomainEvent(new TestEvent()); + } + } + private class Article : AggregateRoot { public string Title { get; private set; } = string.Empty; @@ -111,5 +123,20 @@ public void ClearDomainEvents_Removes_All_Events() // Assert Assert.Empty(article.DomainEvents); } + + [Fact] + public void DomainEvents_IsReadOnly() + { + // Arrange + var aggregate = new TestAggregate(Guid.NewGuid()); + + // Act + var events = aggregate.DomainEvents; + var collection = (ICollection)events; + + // Assert + Assert.Throws(() => collection.Add(new TestEvent())); + Assert.Throws(() => collection.Clear()); + } } -} \ No newline at end of file +}