Update CdkTestHelper to support internal constructors with BindingFlags#6
Merged
Merged
Conversation
- Update generic CreateTestStack methods to use BindingFlags.NonPublic for internal constructor access - Add comprehensive unit tests for both public and internal constructor patterns - Create test helper classes: TestPublicConstructorStack, TestInternalConstructorStack, TestInternalConstructorWithCustomPropsStack - Update README.md with supported constructor patterns and BindingFlags explanation - Update CLAUDE.md with testing patterns documentation for internal constructor support - Enables testing of CDK's default internal constructor pattern without workarounds Technical Details: - Uses BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic for Activator.CreateInstance - Maintains backward compatibility with existing public constructor stacks - Follows common testing library patterns for accessing non-public members - Essential for real-world CDK testing where internal constructors are the default 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull Request Overview
Adds support for internal constructors in the CDK test helper, covers it with unit tests, and updates documentation accordingly.
- Refactored
CreateTestStackmethods to useBindingFlagsfor public and internal constructors - Added extensive tests for public, internal, and custom‐props constructors (both full and minimal helpers)
- Expanded README and CLAUDE documentation with “Supported Constructor Patterns” and testing guidance
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/LayeredCraft.Cdk.Constructs.Tests/Testing/TestingHelpersTests.cs | New fact methods and helper classes to validate public/internal and custom‐props constructors |
| src/LayeredCraft.Cdk.Constructs/Testing/CdkTestHelper.cs | Updated CreateTestStack<TStack> and dual‐generic overload to use BindingFlags, but minimal variants were not updated |
| README.md | Added “Supported Constructor Patterns” section with public/internal examples |
| CLAUDE.md | Enhanced “Testing Patterns” to describe BindingFlags usage and dual‐generic helpers |
Comments suppressed due to low confidence (1)
src/LayeredCraft.Cdk.Constructs/Testing/CdkTestHelper.cs:97
- The
CreateTestStackMinimalmethods were not updated to useBindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, so they will fail for internal constructors. Mirroring the reflection overload fromCreateTestStackinto the minimal methods will ensure internal constructors are also supported.
/// <summary>
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.
Summary
CreateTestStackmethods to support both public and internal constructors usingBindingFlagsTechnical Details
Enhanced Generic Methods:
CreateTestStack<TStack>()andCreateTestStack<TStack, TProps>()to useBindingFlags.NonPublicBindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublicforActivator.CreateInstanceWhy This Matters:
CDK's default pattern is internal constructors for Stack classes. Previously, the generic methods only worked with public constructors, requiring workarounds for real-world CDK stacks.
Test Coverage
TestPublicConstructorStack,TestInternalConstructorStack,TestInternalConstructorWithCustomPropsStackNew Test Classes:
TestPublicConstructorStack: Tests public constructor supportTestInternalConstructorStack: Tests internal constructor supportTestInternalConstructorWithCustomPropsStack: Tests internal constructors with custom props interfacesITestInternalConstructorStackProps&TestInternalConstructorStackProps: Supporting interfacesDocumentation Updates
Benefits
🤖 Generated with Claude Code