Skip to content

Commit 5ef6c24

Browse files
committed
UpdateName only sends events if there is a change.
Add unit tests
1 parent 2dd181d commit 5ef6c24

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

src/Clean.Architecture.Core/ContributorAggregate/Contributor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public Contributor UpdatePhoneNumber(PhoneNumber newPhoneNumber)
1616

1717
public Contributor UpdateName(ContributorName newName)
1818
{
19+
if (Name == newName) return this;
1920
Name = newName;
2021
RegisterDomainEvent(new ContributorNameUpdatedEvent(this));
2122
return this;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Clean.Architecture.Core.ContributorAggregate.Events;
2+
3+
namespace Clean.Architecture.UnitTests.Core.ContributorAggregate;
4+
5+
public class ContributorUpdateName
6+
{
7+
private readonly ContributorName _initialName = ContributorName.From("initial name");
8+
private readonly ContributorName _newName = ContributorName.From("new name");
9+
private Contributor? _testContributor;
10+
private Contributor CreateContributor()
11+
{
12+
return new Contributor(_initialName);
13+
}
14+
15+
[Fact]
16+
public void UpdatesName()
17+
{
18+
_testContributor = CreateContributor();
19+
_testContributor.UpdateName(_newName);
20+
_testContributor.Name.ShouldBe(_newName);
21+
}
22+
23+
[Fact]
24+
public void RegistersDomainEvent()
25+
{
26+
_testContributor = CreateContributor();
27+
_testContributor.UpdateName(_newName);
28+
_testContributor.DomainEvents.Count.ShouldBe(1);
29+
_testContributor.DomainEvents.First().ShouldBeOfType<ContributorNameUpdatedEvent>();
30+
}
31+
32+
[Fact]
33+
public void DoesNotRegisterDomainEventGivenCurrentName()
34+
{
35+
_testContributor = CreateContributor();
36+
_testContributor.UpdateName(_initialName);
37+
_testContributor.DomainEvents.Count.ShouldBe(0);
38+
}
39+
40+
}

0 commit comments

Comments
 (0)