testing

.NET 8 WPF组件宿主应用统一测试策略 - 领域单元测试、ViewModel测试、集成测试和测试基础设施。用于编写实体、ViewModel、仓库测试或设置测试环境。

📖 中文导读:.NET 8 WPF组件宿主应用统一测试策略 - 领域单元测试、ViewModel测试、集成测试和测试基础设施。用于编写实体、ViewModel、仓库测试或设置测试环境。 以下正文为该技能的原始 SKILL.md 内容(英文),上方安装命令可直接使用。

name: testing description: Unified testing strategy for the .NET 8 WPF widget host app - domain unit tests, ViewModel tests, integration tests, and test infrastructure. Use when writing tests for entities, ViewModels, repositories, or setting up test harnesses.

Testing

Overview

Comprehensive testing strategy covering domain logic, UI behavior, and data access with clear patterns for each layer.

Test Project Structure

Project Focus Dependencies
3SC.Domain.Tests Entities, value objects, domain logic Domain only
3SC.UI.Tests ViewModels, commands, converters UI + mocks
3SC.Infrastructure.Tests Repositories, data access Infrastructure + test DB

1. Domain Tests

Scope

  • Entity invariants and behavior
  • Value object equality and validation
  • Domain service logic
  • Pure application use cases

Rules

  • No infrastructure dependencies
  • No UI dependencies
  • Deterministic (no DateTime.Now, no random)
  • Fast (< 1 second per test)

Patterns

[Fact]
public void Widget_WithNullName_ThrowsArgumentException()
{
    Assert.Throws<ArgumentException>(() => new Widget(null!, "key"));
}

2. ViewModel Tests (UI Tests)

Scope

  • Command execution and can-execute
  • Observable property changes
  • State transitions
  • Validation logic

Rules

  • Use NoOp/mock service implementations
  • No WPF runtime dependency
  • Test via WidgetTestHarness when services needed

Patterns

[Fact]
public void Command_WhenExecuted_UpdatesProperty()
{
    var vm = new MyViewModel(new FakeService());
    vm.SomeCommand.Execute(null);
    Assert.True(vm.IsCompleted);
}

3. Integration Tests (Data Tests)

Scope

  • Repository CRUD operations
  • Query behavior
  • Migration validation
  • Transaction handling

Rules

  • Isolated DB per test (in-memory or temp file)
  • Migrations applied before tests
  • Explicit data seeding (no shared fixtures)
  • Proper cleanup in Dispose

Patterns

public class WidgetRepositoryTests : IDisposable
{
    private readonly AppDbContext _context;
    
    public WidgetRepositoryTests()
    {
        _context = DbTestFactory.CreateInMemory();
    }
    
    [Fact]
    public async Task Add_PersistsWidget()
    {
        var repo = new WidgetRepository(_context);
        await repo.AddAsync(new Widget("Test", "key"));
        Assert.Single(await repo.GetAllAsync());
    }
    
    public void Dispose() => _context.Dispose();
}

Definition of Done (DoD)

Domain Tests

  • New domain logic has corresponding unit tests
  • Tests cover happy path + at least one edge/error case
  • Tests are deterministic
  • Tests run in under 1 second each

ViewModel Tests

  • Commands tested for execute and can-execute behavior
  • State changes tested through observable property assertions
  • Test harness updated when new services are added

Integration Tests

  • Each test gets isolated DB instance
  • Migrations applied automatically before tests
  • Repository tests cover CRUD + edge cases
  • Tests clean up resources in Dispose

Test Infrastructure

WidgetTestHarness

Located in 3SC.UI.Tests/WidgetTestHarness.cs - provides NoOp implementations of all widget services for isolated ViewModel testing.

DbTestFactory

Located in 3SC.Infrastructure.Tests/DbTestFactory.cs - creates isolated in-memory or file-based test databases.


Naming Conventions

{MethodName}_{Scenario}_{ExpectedResult}

Examples:

  • GetById_WithValidId_ReturnsWidget
  • Add_WithDuplicateKey_ThrowsException
  • Execute_WhenDisabled_DoesNotRun

Anti-Patterns

  • ❌ Tests depending on other tests' data
  • ❌ Shared mutable state across tests
  • ❌ Testing private methods directly
  • ❌ Fragile UI selectors in ViewModel tests
  • ❌ DateTime.Now or Random in tests