Skip to content

Commit cacc227

Browse files
committed
Adding tests and domain events to Contributor
1 parent dd20fe0 commit cacc227

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

sample/src/NimblePros.SampleToDo.Core/ContributorAggregate/Contributor.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace NimblePros.SampleToDo.Core.ContributorAggregate;
1+
using NimblePros.SampleToDo.Core.ContributorAggregate.Events;
2+
3+
namespace NimblePros.SampleToDo.Core.ContributorAggregate;
24

35
public class Contributor : EntityBase, IAggregateRoot
46
{
@@ -11,6 +13,9 @@ public Contributor(ContributorName name)
1113

1214
public void UpdateName(ContributorName newName)
1315
{
16+
if (Name.Equals(newName)) return;
1417
Name = newName;
18+
this.RegisterDomainEvent(new ContributorNameUpdatedEvent(this));
1519
}
1620
}
21+

sample/src/NimblePros.SampleToDo.Core/ContributorAggregate/Events/ContributorDeletedEvent.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44
/// A domain event that is dispatched whenever a contributor is deleted.
55
/// The DeleteContributorService is used to dispatch this event.
66
/// </summary>
7-
internal class ContributorDeletedEvent : DomainEventBase
7+
internal class ContributorDeletedEvent(int contributorId) : DomainEventBase
88
{
9-
public int ContributorId { get; set; }
10-
11-
public ContributorDeletedEvent(int contributorId)
12-
{
13-
ContributorId = contributorId;
14-
}
9+
public int ContributorId { get; set; } = contributorId;
1510
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NimblePros.SampleToDo.Core.ContributorAggregate.Events;
2+
3+
internal class ContributorNameUpdatedEvent(Contributor contributor) : DomainEventBase
4+
{
5+
public Contributor Contributor { get; private set; } = contributor;
6+
7+
}

sample/tests/NimblePros.SampleToDo.UnitTests/Core/ContributorAggregate/ContributorConstructor.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,41 @@ public void InitializesName()
2020
Assert.Equal(_testName, _testContributor.Name.Value);
2121
}
2222
}
23+
24+
public class ContributorUpdateName
25+
{
26+
private readonly string _testName = "new name";
27+
private Contributor? _testContributor;
28+
29+
private Contributor CreateContributor()
30+
{
31+
return new Contributor(ContributorName.From(_testName));
32+
}
33+
34+
[Fact]
35+
public void DoesNothingGivenSameName()
36+
{
37+
_testContributor = CreateContributor();
38+
var initialEvents = _testContributor.DomainEvents.Count;
39+
40+
var initialHash = _testContributor.GetHashCode();
41+
42+
_testContributor.UpdateName(ContributorName.From(_testName));
43+
44+
Assert.Equal(initialHash, _testContributor.GetHashCode());
45+
Assert.Equal(initialEvents, _testContributor.DomainEvents.Count);
46+
}
47+
48+
[Fact]
49+
public void UpdatesNameAndRegistersEventGivenNewName()
50+
{
51+
_testContributor = CreateContributor();
52+
var initialEvents = _testContributor.DomainEvents.Count;
53+
string newName = "A whole new name";
54+
55+
_testContributor.UpdateName(ContributorName.From(newName));
56+
57+
Assert.Equal(newName, _testContributor.Name.Value);
58+
Assert.Equal(1, _testContributor.DomainEvents.Count);
59+
}
60+
}

0 commit comments

Comments
 (0)