maui-unit-testing

xUnit 测试指南,用于 .NET MAUI 应用的单元测试,覆盖 ViewModel 测试、MAUI 服务模拟、测试项目配置、代码覆盖率以及设备端测试运行器。适用场景包括“MAUI 单元测试”、“xUnit 测试”、“ViewModel 测试”、“模拟 MAUI 服务”、“测试项目设置”、“代码覆盖率”、“设备端测试运行器”、“测试 MAUI 应用”、“IServiceProvider 模拟”和“Moq 在 MAUI 中的使用”。不适用于 UI 自动化测试(请使用 appium-automation)、性能分析(请使用 maui-performance)或依赖注入配置(请使用 maui-dependency-injection)。

仓库最近推送
GitHub Stars
181
Forks
21
Issues / PRs
1

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "maui-unit-testing":https://skillpi.com/zh/skills/deepskill-6430051a478953c93e718fa7423df831de4750236b56d5d932db98cadb643250。安装前核对上游来源和版本:https://github.com/davidortinau/maui-skills。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

github.com

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

xUnit 测试指南,用于 .NET MAUI 应用的单元测试,覆盖 ViewModel 测试、MAUI 服务模拟、测试项目配置、代码覆盖率以及设备端测试运行器。适用场景包括“MAUI 单元测试”、“xUnit 测试”、“ViewModel 测试”、“模拟 MAUI 服务”、“测试项目设置”、“代码覆盖率”、“设备端测试运行器”、“测试 MAUI 应用”、“IServiceProvider 模拟”和“Moq 在 MAUI 中的使用”。不适用于 UI 自动化测试(请使用 appium-automation)、性能分析(请使用 maui-performance)或依赖注入配置(请使用 maui-dependency-injection)。

0 下载

介绍来源: third_party · davidortinau · 0.0.1 参考来源

SkillPi 解读

能完成的任务
翻译待完成,请参阅来源文档。
输入和输出
翻译待完成,请参阅来源文档。
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
Frontmatter
name: maui-unit-testing
description: >
  xUnit testing guidance for .NET MAUI apps — ViewModel testing, mocking MAUI
  services, test project setup, code coverage, and on-device test runners.
  USE FOR: "unit test MAUI", "xUnit test", "ViewModel testing", "mock MAUI services",
  "test project setup", "code coverage", "on-device test runner", "test MAUI app",
  "IServiceProvider mock", "Moq MAUI".
  DO NOT USE FOR: UI automation testing (use appium-automation),
  performance profiling (use maui-performance), or dependency injection setup (use maui-dependency-injection).

.NET MAUI Unit Testing — Gotchas & Best Practices

For project templates, xUnit examples, ViewModel test patterns, and CLI commands, see references/unit-testing-api.md.

⚠️ TFM Trap: Don't Use Platform-Specific TFMs

<!-- ❌ xUnit can't run on platform-specific TFMs -->
<TargetFramework>net9.0-ios</TargetFramework>

<!-- ✅ Use plain .NET TFM for desktop test host -->
<TargetFrameworks>net9.0;net10.0</TargetFrameworks>

⚠️ OutputType Trap: App Project as Test Dependency

If your test project references the app project, the app tries to build as Exe for the test TFM — this fails. Add a conditional OutputType:

<!-- ❌ App .csproj without conditional — breaks test builds -->
<OutputType>Exe</OutputType>

<!-- ✅ Library for test TFM, Exe for platform TFMs -->
<PropertyGroup Condition="'$(TargetFramework)' == 'net9.0'">
  <OutputType>Library</OutputType>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' != 'net9.0'">
  <OutputType>Exe</OutputType>
</PropertyGroup>

Common Mistakes

❌ Using Static MAUI APIs in ViewModels

ViewModels that directly call static MAUI APIs are untestable:

// ❌ Untestable — Shell.Current requires a running MAUI app
public async Task GoToDetail(int id)
    => await Shell.Current.GoToAsync($"detail?id={id}");

// ✅ Inject an interface — fully testable
public class MyViewModel(INavigationService nav)
{
    public async Task GoToDetail(int id)
        => await nav.GoToAsync($"detail?id={id}");
}

Static APIs to wrap behind interfaces:

  • Shell.Current → INavigationService
  • Application.Current → avoid entirely
  • SecureStorage.Default → ISecureStorage
  • Connectivity.Current → IConnectivity

❌ Asserting on UI Bindings Instead of ViewModel State

// ❌ Testing the binding — fragile, needs a running UI
Assert.Equal("Hello", label.Text);

// ✅ Testing the ViewModel — fast, no platform dependency
Assert.Equal("Hello", viewModel.Title);
Assert.True(viewModel.SaveCommand.CanExecute(null));

Mocking Strategy for MAUI Services

MAUI Service Mock Strategy
ISecureStorage Mock<ISecureStorage> — stub GetAsync/SetAsync
IPreferences Mock<IPreferences> — stub Get/Set/Remove
IConnectivity Mock<IConnectivity> — return NetworkAccess
IGeolocation Mock<IGeolocation> — return fixed Location
IFilePicker Mock<IFilePicker> — return FileResult
IMediaPicker Mock<IMediaPicker> — return FileResult
Shell navigation Abstract behind INavigationService
IDispatcher Stub Dispatch to invoke action synchronously

Architecture: Interface-First for Testability

Define service interfaces so ViewModels have zero MAUI platform dependencies:

// ✅ These make your entire ViewModel layer testable
public interface INavigationService
{
    Task GoToAsync(string route);
    Task GoBackAsync();
}

public interface IDialogService
{
    Task<bool> ConfirmAsync(string title, string message);
}

Register implementations in MauiProgram.cs; inject interfaces into ViewModels.

Tips

  • No Application.Current or Shell.Current in ViewModels — wrap in injectable services
  • Use ObservableCollection<T> and [ObservableProperty] (MVVM Toolkit) for testable state
  • Assert on ViewModel properties and CanExecute, not UI bindings
  • Use TaskCompletionSource to test async waiting flows
  • Run dotnet test in CI to catch regressions early
  • On-device tests: Use xunit.runner.devices for real platform APIs (sensors, camera, Bluetooth)

Checklist

  • [ ] Test project targets plain net9.0/net10.0 (not platform-specific TFMs)
  • [ ] App project has conditional OutputType for test TFM
  • [ ] All MAUI static APIs wrapped behind injectable interfaces
  • [ ] ViewModels tested via properties and commands, not UI bindings
  • [ ] IDispatcher mocked to invoke synchronously in tests
  • [ ] dotnet test runs green in CI

当前收录版本

0.0.1

核对来源版本 ↗

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值85/100

亮点

  • 提供 .NET MAUI 单元测试的完整实践指南,涵盖 ViewModel 测试、服务模拟、项目配置等核心场景
  • 面向明确的技术需求,具有广泛实用价值,适合开发者日常使用
触发精准90/100

亮点

  • 触发词覆盖全面,包含 10 个适用场景和 3 个不适用场景,边界清晰
  • 关键词变体完整,如 'unit test MAUI'、'xUnit test'、'ViewModel testing' 等
结构规范90/100

待提升项

  • description 内容重复 原报告严重程度:轻微

    顶层 frontmatter 和 skill_body 中均包含 description,内容一致但存在冗余

    改进建议:建议统一将 description 保留在顶层 frontmatter,skill_body 中可省略或简化引用

亮点

  • 文档结构清晰,分章节组织,包含代码示例、表格和检查清单
  • frontmatter 完整,单文件长度适中,易于阅读和维护
执行可靠90/100

亮点

  • 代码片段和配置建议准确,工具来源(xUnit、Moq)明确
  • 覆盖了常见陷阱(如 TFM 选择、OutputType 配置)并提供解决方案
  • 提供了 CI 集成和端到端测试建议,执行路径可靠
证据可验 (前四项均值)89/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

100 / 100 · skillpi-20261123-no-r3-v1

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。