← 返回
数据分析 中文

FOSMVVM ViewModel Test Generator

Generate ViewModel tests with codable round-trip, versioning stability, and multi-locale translation verification.
生成 ViewModel 测试,支持编解码往返、版本稳定性及多语言翻译验证。
foscomputerservices
数据分析 clawhub v2.0.6 1 版本 100000 Key: 无需
★ 0
Stars
📥 805
下载
💾 27
安装
1
版本
#latest

概述

FOSMVVM ViewModel Test Generator

Generate test files for ViewModels following FOSMVVM testing patterns.

Conceptual Foundation

> For full architecture context, see FOSMVVMArchitecture.md | OpenClaw reference

ViewModel testing in FOSMVVM verifies three critical aspects:

  1. Codable round-trip - ViewModel encodes and decodes without data loss
  2. Versioning stability - Structure hasn't changed unexpectedly
  3. Multi-locale translations - All @LocalizedString properties have values in all supported locales

The LocalizableTestCase protocol provides infrastructure that tests all three in a single call.


When to Use This Skill

  • Creating tests for new ViewModels
  • Adding test coverage to existing ViewModels
  • Verifying localization completeness across locales
  • Testing ViewModels with embedded/nested child ViewModels
  • Verifying @LocalizedSubs substitution behavior

What This Skill Generates

FileLocationPurpose
-------------------------
{Name}ViewModelTests.swiftTests/{Target}Tests/Localization/Test suite conforming to LocalizableTestCase
{Name}ViewModel.ymlTests/{Target}Tests/TestYAML/YAML translations for test (if needed)

The Testing Pattern

Standard Pattern (Most Tests)

For most ViewModels, a single line provides complete coverage:

@Test func dashboardViewModel() throws {
    try expectFullViewModelTests(DashboardViewModel.self)
}

This verifies:

  • Codable encoding/decoding
  • Versioned ViewModel stability
  • Translations exist for all locales (en, es by default)

This is sufficient for the vast majority of ViewModel tests.

Extended Pattern (Specific Formatting Verification)

When testing specific formatting behavior (substitutions, compound strings), add locale-specific assertions:

@Test func greetingWithSubstitution() throws {
    try expectFullViewModelTests(GreetingViewModel.self)

    // Verify specific substitution behavior
    let vm: GreetingViewModel = try .stub()
        .toJSON(encoder: encoder(locale: en))
        .fromJSON()

    #expect(try vm.welcomeMessage.localizedString == "Welcome, John!")
}

This is optional - use only when verifying specific formatting techniques.


LocalizableTestCase Protocol

Test suites conform to LocalizableTestCase to access testing infrastructure:

import FOSFoundation
@testable import FOSMVVM
import FOSTesting
import Foundation
import Testing
@testable import {ViewModelsTarget}

@Suite("My ViewModel Tests")
struct MyViewModelTests: LocalizableTestCase {
    let locStore: LocalizationStore

    init() throws {
        self.locStore = try Self.loadLocalizationStore(
            bundle: {ViewModelsTarget}.resourceAccess,
            resourceDirectoryName: ""
        )
    }
}

The {ViewModelsTarget}.resourceAccess is the resource accessor defined when creating the ViewModels SPM target (via FOSResourceAccessor build tool plugin).

What LocalizableTestCase Provides

Property/MethodPurpose
--------------------------
locStoreRequired - the localization store
localesOptional - locales to test (default: en, es)
encoder(locale:)Creates a localizing JSONEncoder
en, es, enGB, enUSLocale constants

Testing Methods

MethodUse When
------------------
expectFullViewModelTests(_:)Primary - complete ViewModel testing
expectTranslations(_:)Translation-only verification
expectFullFieldValidationModelTests(_:)Testing FieldValidationModel types
expectFullFormFieldTests(_:)Testing FormField instances
expectCodable(_:encoder:)Codable round-trip only
expectVersionedViewModel(_:encoder:)Versioning stability only

YAML Requirements

ViewModels with @LocalizedString

Every ViewModel with @LocalizedString properties needs YAML entries:

@ViewModel
public struct DashboardViewModel: RequestableViewModel {
    @LocalizedString public var pageTitle      // Needs YAML entry
    @LocalizedString public var emptyMessage   // Needs YAML entry
    public let itemCount: Int                   // No YAML needed
}
# DashboardViewModel.yml
en:
  DashboardViewModel:
    pageTitle: "Dashboard"
    emptyMessage: "No items yet"

es:
  DashboardViewModel:
    pageTitle: "Tablero"
    emptyMessage: "No hay elementos todavía"

Embedded ViewModels

When a ViewModel contains child ViewModels, all types in the hierarchy need YAML entries:

@ViewModel
public struct BoardViewModel: RequestableViewModel {
    @LocalizedString public var title
    public let cards: [CardViewModel]  // Child ViewModel
}

@ViewModel
public struct CardViewModel {
    @LocalizedString public var cardTitle
}

Both BoardViewModel and CardViewModel need YAML entries (can be in same or separate files).

Private Test ViewModels

When tests define private ViewModel structs for testing specific scenarios, those also need YAML:

// In test file
private struct TestParentViewModel: ViewModel {
    @LocalizedString var title
    let children: [TestChildViewModel]
}

private struct TestChildViewModel: ViewModel {
    @LocalizedString var label
}

Add entries to a test YAML file for these private types.


How to Use This Skill

Invocation:

/fosmvvm-viewmodel-test-generator

Prerequisites:

  • ViewModel structure understood from conversation context
  • Localization properties identified (@LocalizedString, @LocalizedSubs, etc.)
  • YAML localization files exist or will be created
  • Child ViewModels identified (if any)

Workflow integration:

This skill is used when adding test coverage for ViewModels. The skill references conversation context automatically—no file paths or Q&A needed. Typically follows fosmvvm-viewmodel-generator.

Pattern Implementation

This skill references conversation context to determine test structure:

ViewModel Analysis

From conversation context, the skill identifies:

  • ViewModels to test (from prior discussion or codebase)
  • Localization requirements (@LocalizedString properties)
  • Child ViewModels (embedded within parent)
  • Substitution behavior (@LocalizedSubs needing specific verification)

YAML Coverage Check

Verifies completeness:

  • ViewModel YAML entries (all @LocalizedString properties)
  • Child ViewModel entries (nested types)
  • Locale coverage (en, es, or project-specific locales)

Test File Generation

Creates test suite with:

  • LocalizableTestCase conformance
  • Localization store initialization
  • expectFullViewModelTests() calls for each ViewModel
  • Optional specific formatting tests (substitutions, compound strings)

Context Sources

Skill references information from:

  • Prior conversation: ViewModels discussed or recently created
  • ViewModel code: If Claude has read ViewModel files into context
  • YAML files: From codebase analysis of existing localizations
  • Test patterns: From existing test files in project

File Templates

See reference.md for complete file templates.


Common Scenarios

Testing a Single Top-Level ViewModel

@Test func dashboardViewModel() throws {
    try expectFullViewModelTests(DashboardViewModel.self)
}

Testing Multiple Related ViewModels

@Test func boardViewModels() throws {
    try expectFullViewModelTests(BoardViewModel.self)
    try expectFullViewModelTests(ColumnViewModel.self)
    try expectFullViewModelTests(CardViewModel.self)
}

Testing with Custom Locales

var locales: Set<Locale> { [en, es, enGB] }  // Override default

@Test func multiLocaleViewModel() throws {
    try expectFullViewModelTests(MyViewModel.self)
    // Tests en, es, AND en-GB
}

Testing Substitution Behavior

@Test func greetingSubstitutions() throws {
    try expectFullViewModelTests(GreetingViewModel.self)

    let vm: GreetingViewModel = try .stub(userName: "Alice")
        .toJSON(encoder: encoder(locale: en))
        .fromJSON()

    #expect(try vm.welcomeMessage.localizedString == "Welcome, Alice!")
}

Testing Embedded ViewModels

@Test func parentWithChildren() throws {
    // Tests parent AND verifies children can be encoded/decoded
    try expectFullViewModelTests(ParentViewModel.self)

    // Optionally verify specific child values
    let vm: ParentViewModel = try .stub()
        .toJSON(encoder: encoder(locale: en))
        .fromJSON()

    #expect(try vm.children[0].label.localizedString == "Child 1")
}

Troubleshooting

"Missing Translation" Error

FOSLocalizableError: _pageTitle -- Missing Translation -- en

Cause: YAML entry missing for a @LocalizedString property.

Fix: Add the property to the YAML file:

en:
  MyViewModel:
    pageTitle: "Page Title"  # Add this

"Is pending localization" Error

Cause: The ViewModel wasn't encoded with a localizing encoder.

Fix: Ensure using encoder(locale:) or expectFullViewModelTests().

Test Passes But Translations Seem Wrong

Cause: YAML values exist but may have typos or wrong content.

Fix: Add specific assertions to verify exact values:

let vm = try .stub().toJSON(encoder: encoder(locale: en)).fromJSON()
#expect(try vm.title.localizedString == "Expected Value")

Naming Conventions

ConceptConventionExample
------------------------------
Test suite{Feature}ViewModelTestsDashboardViewModelTests
Test file{Feature}ViewModelTests.swiftDashboardViewModelTests.swift
YAML file{ViewModelName}.ymlDashboardViewModel.yml
Test method{viewModelName}() or descriptivedashboardViewModel()

See Also


Version History

VersionDateChanges
------------------------
1.02025-01-02Initial skill
1.12026-01-19Updated LocalizableTestCase example to use {ViewModelsTarget}.resourceAccess pattern.
1.22026-01-24Update to context-aware approach (remove file-parsing/Q&A). Skill references conversation context instead of asking questions or accepting file paths.

版本历史

共 1 个版本

  • v2.0.6 当前
    2026-03-29 14:26 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

productivity

FOSMVVM Fluent DataModel Generator

foscomputerservices
生成带迁移和测试的 Fluent DataModel,用于 FOSMVVM 服务器端持久化,包括关联关系和系统分配的字段,基于现有的...
★ 0 📥 1,109
data-analysis

Excel / XLSX

ivangdavila
创建、检查和编辑 Microsoft Excel 工作簿及 XLSX 文件,支持可靠的公式、日期、类型、格式、重算及模板保留功能。
★ 367 📥 139,997
data-analysis

A股量化 AkShare

mbpz
A股量化数据分析工具,基于AkShare库获取A股行情、财务数据、板块信息等。用于回答关于A股股票查询、行情数据、财务分析、选股等问题。
★ 163 📥 59,692