Add unit tests for LazyInnerEvent#31
Conversation
Co-authored-by: Hissal <89788928+Hissal@users.noreply.github.com>
Hissal
left a comment
There was a problem hiding this comment.
Consider making frequently used substitutes class level fields.
If each phase of AAA in a test is only one line the AAA comments can be omitted and each line can be back to back without empty lines in between.
| // Arrange | ||
| var sut = new LazyInnerEvent<int>(); | ||
|
|
||
| // Act | ||
| sut.Publish(42); | ||
|
|
||
| // Assert | ||
| sut.HandlerCount.Should().Be(0); |
There was a problem hiding this comment.
No need for AAA when each phase is one line. And each line does not need and empty line in between.
There was a problem hiding this comment.
Refactored to remove AAA comments for single-line phases and removed empty lines between them. Kept AAA comments only for tests with multi-line phases. (b19884d)
…ases Co-authored-by: Hissal <89788928+Hissal@users.noreply.github.com>
Refactored test formatting - removed AAA comments and empty lines for single-line phases. Note: Each test creates a fresh |
Hissal
left a comment
There was a problem hiding this comment.
Frequently uised types such a sut (if same ctor params used) can be a class level field to reduce length of tests.
Similarly frequently used substitutes can also be class level fields such as a handler.
| [Fact] | ||
| public void Constructor_WithoutParameter_UsesGlobalConfig() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| sut.Should().NotBeNull(); | ||
| sut.IsDisposed.Should().BeFalse(); | ||
| sut.HandlerCount.Should().Be(0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Constructor_WithExpectedSubscriberCount_InitializesCorrectly() { | ||
| var sut = new LazyInnerEvent<int>(10); | ||
| sut.Should().NotBeNull(); | ||
| sut.IsDisposed.Should().BeFalse(); | ||
| sut.HandlerCount.Should().Be(0); | ||
| } |
There was a problem hiding this comment.
if each phase is longer than 1 line each phase should be seperated by 1 empty line.
There was a problem hiding this comment.
Added empty lines between multi-line AAA phases. (0a6da7e)
| [Fact] | ||
| public void Publish_AfterSubscription_SendsMessageToHandlers() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| var handler = Substitute.For<IEventHandler<int>>(); | ||
| sut.Subscribe(handler); | ||
| sut.Publish(69); | ||
| handler.Received(1).Handle(69); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Subscribe_MaterializesCore_AndReturnsDisposable() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| var handler = Substitute.For<IEventHandler<int>>(); | ||
| var subscription = sut.Subscribe(handler); | ||
| subscription.Should().NotBeNull(); | ||
| subscription.Should().NotBe(Disposable.Empty); | ||
| sut.HandlerCount.Should().Be(1); | ||
| } |
There was a problem hiding this comment.
if each phase is longer than 1 line each phase should be seperated by 1 empty line.
There was a problem hiding this comment.
Added empty lines between multi-line AAA phases. (0a6da7e)
| [Fact] | ||
| public void Subscribe_AfterDispose_ReturnsEmptyDisposable() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| var handler = Substitute.For<IEventHandler<int>>(); | ||
| sut.Dispose(); | ||
| var subscription = sut.Subscribe(handler); | ||
| subscription.Should().Be(Disposable.Empty); | ||
| sut.HandlerCount.Should().Be(0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Subscribe_AfterDispose_HandlerDoesNotReceiveMessages() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| var handler = Substitute.For<IEventHandler<int>>(); | ||
| sut.Dispose(); | ||
| sut.Subscribe(handler); | ||
| sut.Publish(42); | ||
| handler.DidNotReceive().Handle(Arg.Any<int>()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Publish_AfterDispose_DoesNotSendMessageToHandlers() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| var handler = Substitute.For<IEventHandler<int>>(); | ||
| sut.Subscribe(handler); | ||
| sut.Dispose(); | ||
| sut.Publish(69); | ||
| handler.DidNotReceive().Handle(Arg.Any<int>()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Publish_AfterDisposeBeforeMaterialization_DoesNothing() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| sut.Dispose(); | ||
| sut.Publish(42); | ||
| sut.IsDisposed.Should().BeTrue(); | ||
| } |
There was a problem hiding this comment.
if each phase is longer than 1 line each phase should be seperated by 1 empty line.
There was a problem hiding this comment.
Added empty lines between multi-line AAA phases. (0a6da7e)
|
|
||
| [Fact] | ||
| public void IsDisposed_AfterDisposeOfMaterializedCore_ReturnsTrue() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| var handler = Substitute.For<IEventHandler<int>>(); | ||
| sut.Subscribe(handler); | ||
| sut.Dispose(); | ||
| sut.IsDisposed.Should().BeTrue(); | ||
| } |
There was a problem hiding this comment.
if each phase is longer than 1 line each phase should be seperated by 1 empty line.
There was a problem hiding this comment.
Added empty lines between multi-line AAA phases. (0a6da7e)
| [Fact] | ||
| public void ClearHandlers_AfterDispose_DoesNothing() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| var handler = Substitute.For<IEventHandler<int>>(); | ||
| sut.Subscribe(handler); | ||
| sut.Dispose(); | ||
| sut.ClearHandlers(); | ||
| sut.IsDisposed.Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Dispose_Multiple_DoesNotThrow() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| sut.Dispose(); | ||
| sut.Dispose(); | ||
| sut.IsDisposed.Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Dispose_WithSubscribedHandlers_DisposesCore() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| var handler = Substitute.For<IEventHandler<int>>(); | ||
| sut.Subscribe(handler); | ||
| sut.Dispose(); | ||
| sut.IsDisposed.Should().BeTrue(); | ||
| sut.HandlerCount.Should().Be(0); | ||
| } |
There was a problem hiding this comment.
if each phase is longer than 1 line each phase should be seperated by 1 empty line.
There was a problem hiding this comment.
Added empty lines between multi-line AAA phases. (0a6da7e)
| [Fact] | ||
| public void Next_AfterSetNext_ReturnsSetValue() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| var nextEvent = new LazyInnerEvent<int>(); | ||
| sut.SetNext(nextEvent); | ||
| var next = sut.Next; | ||
| next.Should().Be(nextEvent); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetNext_BeforeMaterialization_SetsCore() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| var nextEvent = new LazyInnerEvent<int>(); | ||
| sut.SetNext(nextEvent); | ||
| sut.Next.Should().Be(nextEvent); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetNext_AfterMaterialization_ThrowsInvalidOperationException() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| var handler = Substitute.For<IEventHandler<int>>(); | ||
| sut.Subscribe(handler); | ||
| var nextEvent = new LazyInnerEvent<int>(); | ||
| var act = () => sut.SetNext(nextEvent); | ||
| act.Should().Throw<InvalidOperationException>() | ||
| .WithMessage("Inner already materialized; cannot set after first use."); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetNext_AfterDispose_ThrowsObjectDisposedException() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| sut.Dispose(); | ||
| var nextEvent = new LazyInnerEvent<int>(); | ||
| var act = () => sut.SetNext(nextEvent); | ||
| act.Should().Throw<ObjectDisposedException>(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetNext_AfterPublishWithoutHandlers_SucceedsBecauseNoMaterialization() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| sut.Publish(42); | ||
| var nextEvent = new LazyInnerEvent<int>(); | ||
| sut.SetNext(nextEvent); | ||
| sut.Next.Should().Be(nextEvent); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Publish_ThroughSetNextPipeline_SendsMessageToHandlers() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| var nextEvent = new LazyInnerEvent<int>(); | ||
| var handler = Substitute.For<IEventHandler<int>>(); | ||
| sut.SetNext(nextEvent); | ||
| nextEvent.Subscribe(handler); | ||
| sut.Publish(69); | ||
| handler.Received(1).Handle(69); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Subscribe_ThroughPipeline_AddsHandlerToSetNext() { | ||
| var sut = new LazyInnerEvent<int>(); | ||
| var nextEvent = new LazyInnerEvent<int>(); | ||
| var handler = Substitute.For<IEventHandler<int>>(); | ||
| sut.SetNext(nextEvent); | ||
| sut.Subscribe(handler); | ||
| sut.HandlerCount.Should().Be(1); | ||
| } |
There was a problem hiding this comment.
if each phase is longer than 1 line each phase should be seperated by 1 empty line.
There was a problem hiding this comment.
Added empty lines between multi-line AAA phases. (0a6da7e)
Co-authored-by: Hissal <89788928+Hissal@users.noreply.github.com>
For LazyInnerEvent tests, each test creates fresh instances to maintain test isolation and support different scenarios (various constructor parameters, disposal states, materialization states). Unlike EventCore which uses a readonly class-level sut with consistent state, LazyInnerEvent tests require different initialization contexts per test. Class-level fields would risk state leakage between tests and reduce test clarity about the specific setup needed for each scenario. |
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.