test: DRY up ResponseIdValidationServiceTests#1078
Merged
Conversation
Replace per-test CreateCache/CreateService helpers and local variables with a shared _service field initialized via the production parameterless constructor. Use [After(Test)] for TUnit-idiomatic cleanup and implement IDisposable with GC.SuppressFinalize to satisfy CA1001/CA1816 analyzers. Removes ~30 lines of repetitive setup boilerplate across 9 tests.
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors ResponseIdValidationServiceTests in the test suite to remove repeated per-test setup while still exercising the production ResponseIdValidationService constructor behavior (including bounded MemoryCache sizing).
Changes:
- Replaced per-test
MemoryCache/service factory setup with a single_servicefield initialized via the production parameterless constructor. - Added test cleanup via
[After(Test)]and implementedIDisposableto satisfy disposable-field analyzers. - Removed the explicit
Microsoft.Extensions.Caching.Memorydependency from the test file.
Add a _disposed guard to prevent double-dispose of the underlying MemoryCache, satisfying the framework design guideline that Dispose() must be callable multiple times without throwing. The test class already uses the correct TUnit pattern ([After(Test)] with CA1001 suppressed), so no test changes are needed.
…nseIdValidationServiceTests TUnit0023 recognizes IDisposable.Dispose() as a valid teardown path alongside [After(Test)]. Using IDisposable directly satisfies both CA1001 (Roslyn) and TUnit0023 with a single, standard C# pattern — no suppression or TUnit-specific hook needed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
Refactored
ResponseIdValidationServiceTeststo eliminate repetitive setup boilerplate across all 9 tests.Before: Each test created its own
MemoryCacheandResponseIdValidationServicevia two static factory helpers, resulting in a 2-line setup block in every test body.After: A single
_servicefield is initialized once using the production parameterless constructor (which already creates a boundedMemoryCachewithSizeLimit = 10_000). Cleanup is handled via[After(Test)](TUnit-idiomatic) plusIDisposable/GC.SuppressFinalizeto satisfy CA1001/CA1816 analyzers.Why
using Microsoft.Extensions.Caching.Memoryimport entirely — tests no longer need to know cache configuration detailsSizeLimitis inherently exercised without duplication