Skip to content

Conversation

@Krishanthaudayakumara
Copy link
Owner

@Krishanthaudayakumara Krishanthaudayakumara commented Aug 29, 2025

Description / Motivation

Issue

The integration test suite was using the deprecated TestServerBuilder class, which caused several critical issues:

  • DirectoryNotFoundException: Tests were failing due to incorrect content root path resolution when running in CI/CD environments
  • Service Registration Issues: Scoped services like IOptionsSnapshot<HttpLayoutRequestHandlerOptions> couldn't be resolved from the root service provider
  • Test Isolation Problems: Pages middleware was interfering with standard model binding tests
  • Obsolete Dependencies: Using deprecated testing patterns that are no longer supported in modern ASP.NET Core

Solution

Migrated the entire integration test infrastructure to use the modern WebApplicationFactory<T> pattern with the following improvements:

Core Infrastructure Changes:

  • Fixed Path Resolution: Replaced assembly-based path detection with Directory.GetCurrentDirectory() to resolve content root issues
  • Conditional Test Configuration: Implemented smart detection of Pages vs Standard tests using typeof(T).Name.Contains("Pages")
  • Proper Service Mocking: Fixed scoped service resolution by using NSubstitute mocks for IOptionsSnapshot dependencies
  • Separate Program Classes: Created dedicated test program classes for different test scenarios

Test Architecture Improvements:

  • Pages Tests: Uses TestPagesProgram with GraphQL handlers, Pages middleware, and editing functionality
  • Standard Tests: Uses TestModelBindingProgram with HTTP handlers and standard rendering engine middleware
  • Mock Integration: Properly configured mock GraphQL and HTTP clients for test isolation

Code Modernization:

  • Primary Constructor: Applied C# 12 primary constructor syntax for cleaner dependency injection
  • Factory Pattern: Replaced manual TestServer instantiation with WebApplicationFactory<T> dependency injection
  • Simplified Test Setup: Eliminated boilerplate code and improved test readability

Files Changed:

Modified:
├── TestWebApplicationFactory.cs - Complete overhaul with conditional configuration
├── ModelBindingFixture.cs - Migrated to WebApplicationFactory pattern
└── TestPagesProgram.cs - Cleaned up obsolete configurations

New:
└── TestModelBindingProgram.cs - Dedicated program for model binding tests

Test Results:

  • All Pages Tests: 8/8 passing (PagesEditingFixture + PagesSetupRoutingFixture)
  • All Model Binding Tests: 6/6 passing
  • No Regressions: All existing functionality preserved
  • Clean Build: Resolved all compilation errors and dependency issues

Technical Details:

  • Handler Configuration: Proper setup of GraphQL vs HTTP layout service handlers
  • Middleware Pipeline: Correct middleware registration for different test scenarios
  • Mock Management: Centralized mock configuration through factory pattern
  • Service Lifetime: Resolved scoped service injection issues in test environment

Testing

  • The Unit & Intergration tests are passing.
  • I have added the necessary tests to cover my changes.

Terms

- Replace obsolete TestServerBuilder with TestWebApplicationFactory<TestModelBindingProgram>
- Implement constructor injection pattern with IClassFixture
- Utilize factory's built-in MockClientHandler for HTTP mocking
- Modernize test structure to follow current ASP.NET Core testing practices
- Maintain existing test coverage while improving test isolation
…ax and remove unnecessary spaces in TestWebApplicationFactory

This comment was marked as outdated.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR migrates integration tests from the deprecated TestServerBuilder to the modern WebApplicationFactory<T> pattern, resolving path resolution issues, service registration problems, and test isolation concerns in CI/CD environments.

Key Changes:

  • Replaced deprecated TestServerBuilder with WebApplicationFactory<T> pattern
  • Implemented conditional test configuration based on test type (Pages vs Standard)
  • Fixed content root path resolution and scoped service injection issues

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
TestWebApplicationFactory.cs Complete overhaul with conditional configuration for Pages and Standard tests
IPagesTestProgram.cs New marker interface for Pages test identification
IStandardTestProgram.cs New marker interface for Standard test identification
TestPagesProgram.cs Updated to implement IPagesTestProgram interface and fix content root
TestModelBindingProgram.cs New dedicated program class for model binding tests
ModelBindingFixture.cs Migrated from TestServer to WebApplicationFactory pattern

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

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