Skip to content

Add NSubstituteDefaultFactory configuration documentation #893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Aug 5, 2025

Addresses #837 by adding comprehensive documentation for customizing NSubstitute's auto value generation using the NSubstituteDefaultFactory configuration pattern.

What this adds

This PR adds a new "Customizing auto values" section to the auto-and-recursive-mocks documentation that explains how to extend NSubstitute's behavior following the open/closed principle. The documentation was requested to make the valuable guidance from PR #705 discussion officially discoverable.

Key features documented

  • Container customization: How to use NSubstituteDefaultFactory.DefaultContainer.Customize() to create a derived container
  • Decoration pattern: Using .Decorate<T>() to enhance existing implementations without replacing them
  • Module Initializers: Proper setup using [ModuleInitializer] for early configuration
  • Complete working example: Custom Task auto value provider that returns failed tasks instead of default completed tasks

Example usage

[ModuleInitializer]
internal static void ConfigureNSubstitute()
{
    var customizedContainer = NSubstituteDefaultFactory.DefaultContainer.Customize();
    customizedContainer.Decorate<IAutoValueProvidersFactory>(
        (factory, _resolver) => new CustomAutoValueProvidersFactory(factory));

    SubstitutionContext.Current = customizedContainer.Resolve<ISubstitutionContext>();
}

This enables scenarios like:

  • Unity's UniTask support instead of standard Task
  • Custom async patterns and domain-specific value generation
  • Enhanced testing scenarios with specific auto-generated values

The documentation includes usage examples, key architectural concepts, and maintains consistency with NSubstitute's existing documentation style.

Note: This is purely documentation - no code changes to NSubstitute itself were needed as the API already existed.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…-recursive-mocks section

Co-authored-by: 304NotModified <5808377+304NotModified@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Fix @nsubstitute/NSubstitute/issues/837 Add NSubstituteDefaultFactory configuration documentation Aug 5, 2025
@Copilot Copilot AI requested a review from 304NotModified August 5, 2025 22:53
Copilot finished work on behalf of 304NotModified August 5, 2025 22:53
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