Skip to content

Update CdkTestHelper to support internal constructors with BindingFlags#6

Merged
ncipollina merged 1 commit into
mainfrom
feature/internal-constructor-support
Jul 4, 2025
Merged

Update CdkTestHelper to support internal constructors with BindingFlags#6
ncipollina merged 1 commit into
mainfrom
feature/internal-constructor-support

Conversation

@ncipollina

Copy link
Copy Markdown
Contributor

Summary

  • Update generic CreateTestStack methods to support both public and internal constructors using BindingFlags
  • Add comprehensive unit tests covering all constructor patterns
  • Update documentation in README.md and CLAUDE.md
  • Enable seamless testing of CDK's default internal constructor pattern

Technical Details

Enhanced Generic Methods:

  • Updated CreateTestStack<TStack>() and CreateTestStack<TStack, TProps>() to use BindingFlags.NonPublic
  • Uses BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic for Activator.CreateInstance
  • Maintains backward compatibility with existing public constructor stacks

Why 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.

// ✅ Now works - CDK default pattern
public class MyStack : Stack
{
    internal MyStack(Construct scope, string id, IStackProps props) : base(scope, id, props) { }
}

// ✅ Still works - Custom public pattern  
public class MyStack : Stack
{
    public MyStack(Construct scope, string id, IStackProps props) : base(scope, id, props) { }
}

Test Coverage

  • Added 8 new comprehensive unit tests
  • Created test helper classes: TestPublicConstructorStack, TestInternalConstructorStack, TestInternalConstructorWithCustomPropsStack
  • Integration tests showing end-to-end workflows with constructs
  • Covers both single and dual generic method patterns

New Test Classes:

  • TestPublicConstructorStack: Tests public constructor support
  • TestInternalConstructorStack: Tests internal constructor support
  • TestInternalConstructorWithCustomPropsStack: Tests internal constructors with custom props interfaces
  • ITestInternalConstructorStackProps & TestInternalConstructorStackProps: Supporting interfaces

Documentation Updates

  • README.md: Added "Supported Constructor Patterns" section with examples
  • CLAUDE.md: Added comprehensive "Testing Patterns" section explaining BindingFlags usage
  • Clear guidance on when and why to use internal constructor support

Benefits

  • Real-World Ready: Works with CDK's default internal constructor pattern
  • No Workarounds: Direct testing of custom stacks without creating unused base stacks
  • Standards Compliance: Follows common testing library patterns for accessing non-public members
  • Type Safety: Maintains full IntelliSense support and compile-time checks

🤖 Generated with Claude Code

- 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>
@ncipollina
ncipollina requested a review from Copilot July 4, 2025 01:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds support for internal constructors in the CDK test helper, covers it with unit tests, and updates documentation accordingly.

  • Refactored CreateTestStack methods to use BindingFlags for 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 CreateTestStackMinimal methods were not updated to use BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, so they will fail for internal constructors. Mirroring the reflection overload from CreateTestStack into the minimal methods will ensure internal constructors are also supported.
    /// <summary>

@ncipollina
ncipollina merged commit 230513e into main Jul 4, 2025
1 check passed
@ncipollina
ncipollina deleted the feature/internal-constructor-support branch July 4, 2025 01:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants