Add dual generic parameter methods to CdkTestHelper for exact type matching#5
Conversation
…tching - Add CreateTestStack<TStack, TProps> and CreateTestStackMinimal<TStack, TProps> methods - Solve Activator.CreateInstance type matching issues when custom stack constructors expect specific props interfaces - Add comprehensive unit tests for dual generic methods with TestAdvancedStack and TestCustomStackProps - Update README.md with "Testing with Custom Stack Props Types" section explaining when to use dual generics - Maintain backward compatibility with existing single generic methods 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Pull Request Overview
Adds dual-generic overloads to the CdkTestHelper for exact props type matching, along with comprehensive tests and documentation updates.
- Introduces
CreateTestStack<TStack, TProps>andCreateTestStackMinimal<TStack, TProps>for custom props interfaces - Adds unit tests and helper interfaces/classes to cover dual-generic scenarios
- Updates README with a "Testing with Custom Stack Props Types" example section
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| test/LayeredCraft.Cdk.Constructs.Tests/Testing/TestingHelpersTests.cs | Added and refactored tests to exercise the new dual-generic methods |
| src/LayeredCraft.Cdk.Constructs/Testing/CdkTestHelper.cs | Implemented dual-generic CreateTestStack and CreateTestStackMinimal |
| README.md | Documented dual-generic usage with a full example |
Comments suppressed due to low confidence (2)
src/LayeredCraft.Cdk.Constructs/Testing/CdkTestHelper.cs:85
- Consider adding error handling or a descriptive exception when reflection fails to instantiate
TStack, sinceActivator.CreateInstancewill throw a generic exception if no matching constructor is found.
var stack = (TStack)Activator.CreateInstance(typeof(TStack), app, stackName, stackProps)!;
test/LayeredCraft.Cdk.Constructs.Tests/Testing/TestingHelpersTests.cs:437
- The test sets custom tags in
TestCustomStackPropsbut doesn't assert that those tags are applied to the stack; consider adding assertions againststack.Tagsto verify propagation.
{ "TestMethod", "DualGeneric" },
| /// <summary> | ||
| /// Creates a test CDK app and custom stack type with the specified custom props type. | ||
| /// This method uses reflection to instantiate the custom stack type with custom props. | ||
| /// Note: You must create the Template.FromStack(stack) after adding constructs to the stack. | ||
| /// </summary> | ||
| /// <typeparam name="TStack">The custom stack type that inherits from Stack</typeparam> | ||
| /// <typeparam name="TProps">The custom props type that implements IStackProps</typeparam> | ||
| /// <param name="stackName">The name of the test stack</param> | ||
| /// <param name="stackProps">Custom stack props of type TProps</param> | ||
| /// <returns>Tuple containing the app and the custom stack instance</returns> |
There was a problem hiding this comment.
[nitpick] The XML doc comments for the dual-generic overloads largely duplicate those of the single-generic methods; consider refactoring or sharing common documentation to reduce maintenance overhead.
| /// <summary> | |
| /// Creates a test CDK app and custom stack type with the specified custom props type. | |
| /// This method uses reflection to instantiate the custom stack type with custom props. | |
| /// Note: You must create the Template.FromStack(stack) after adding constructs to the stack. | |
| /// </summary> | |
| /// <typeparam name="TStack">The custom stack type that inherits from Stack</typeparam> | |
| /// <typeparam name="TProps">The custom props type that implements IStackProps</typeparam> | |
| /// <param name="stackName">The name of the test stack</param> | |
| /// <param name="stackProps">Custom stack props of type TProps</param> | |
| /// <returns>Tuple containing the app and the custom stack instance</returns> | |
| /// <inheritdoc cref="CreateTestStack(string, IStackProps)" /> | |
| /// <typeparam name="TStack">The custom stack type that inherits from Stack</typeparam> | |
| /// <typeparam name="TProps">The custom props type that implements IStackProps</typeparam> |
| var customStack = stack; | ||
| customStack.CustomProperty.Should().Be("CustomValue"); |
There was a problem hiding this comment.
[nitpick] The alias customStack is redundant since stack is already the correct type; you can assert directly on stack to simplify the test.
| var customStack = stack; | |
| customStack.CustomProperty.Should().Be("CustomValue"); | |
| stack.CustomProperty.Should().Be("CustomValue"); |
Summary
CreateTestStack<TStack, TProps>andCreateTestStackMinimal<TStack, TProps>to CdkTestHelperActivator.CreateInstancetype matching issues when custom stack constructors expect specific props interfaces (not justIStackProps)Technical Details
The new dual generic methods enable exact type matching for custom stack constructors that expect specific props interfaces. This solves issues where
Activator.CreateInstancefails when trying to passIStackPropsto constructors expecting concrete interface types likeILightsaberStackPropsorIInfraStackProps.New Methods Added:
CreateTestStack<TStack, TProps>(string stackName, TProps stackProps)CreateTestStackMinimal<TStack, TProps>(string stackName, TProps stackProps)When to Use:
ILightsaberStackProps)Activator.CreateInstanceTest Coverage
TestAdvancedStack,ITestCustomStackProps,TestCustomStackPropsDocumentation Updates
🤖 Generated with Claude Code