mcpField
R
roblox-testing

roblox-testing

github.com/tabooharmony测试与质量150145 次安装2 周前更新
代码仓库

关于 roblox-testing

面向 Roblox 开发者编写 Luau 单元和集成测试的技能,涵盖 TestEZ、服务模拟、依赖注入,以及使用 Lune 的 CI/CD 工作流。

主要功能

  1. 01TestEZ 测试结构与断言
  2. 02通过依赖注入提升可测试性
  3. 03模拟 Roblox 服务与信号
  4. 04基于 Lune 的 CI/CD 流水线
  5. 05测试优先级与常见陷阱

适用场景

  1. 01使用 TestEZ 测试 Luau 模块
  2. 02模拟 DataStore 和远程事件
  3. 03使用 Lune 在 CI 中运行测试
  4. 04重构代码以注入依赖

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/tabooharmony/roblox-brain --skill roblox-testing

安装后在 Claude Code 中输入/roblox-testing确认已加载。

附带文件references/full.md

详细介绍

何时加载

在编写单元/集成测试、模拟 Roblox 服务、使用 Lune 设置 CI/CD,或通过依赖注入重构代码以提高可测试性时加载。语法相关内容交由 roblox-luau-core 处理,类型设计交由 roblox-luau-types 处理,架构相关内容交由 roblox-luau-patterns 处理,验证相关内容交由 roblox-security 处理。

快速参考

TestEZ(BDD 框架)
  • 安装:在 wally.toml 中添加 [dev-dependencies] TestEZ = "roblox/[email protected]"
  • 文件:*.spec.luau,与源文件放在同一目录
  • 代码块:describe → it → beforeEach/afterEach
  • 断言:expect(x).to.equal(y)、.to.be.near(n, ε)、.to.throw()
依赖注入
-- BAD: untestable outside Studio
local store = game:GetService("DataStoreService"):GetDataStore("PlayerData")
-- GOOD: inject dependency
local function saveData(player, data, dataStore)
    dataStore:SetAsync("player_" .. player.UserId, data)
end
-- Module init pattern:
function Manager.init(dataStoreService)
    _dss = dataStoreService or game:GetService("DataStoreService")
end
模拟要点
-- MockDataStore: in-memory Get/Set/Update
local M = {}
function M.new() return setmetatable({_data={}}, {__index=M}) end
function M:GetAsync(k) return self._data[k] end
function M:SetAsync(k,v) self._data[k]=v end
function M:GetDataStore() return self end
-- MockRemoteEvent: OnServerEvent + FireServer
-- MockSignal: Connect/Fire with Connected flag
使用 Lune 设置 CI/CD
# aftman.toml
[tools]
lune = "lune-org/[email protected]"

流水线:selene src/ → stylua --check src/ → luau-lsp analyze → lune run tests/run.luau

-- tests/run.luau
local TestEZ = require("@testez")
local results = TestEZ.TestBootstrap:run({game:GetService("ReplicatedStorage")}, TestEZ.Reporters.TextReporter)
if results.failureCount > 0 then os.exit(1) end
测试优先级
  1. DataStore 保存/加载往返(防止数据丢失)
  2. 变现 ProcessReceipt(幂等性)
  3. 核心数学逻辑(伤害、增益、经济)
  4. 服务端验证(远程漏洞利用)
常见问题
  • 使用 beforeEach 重置状态;切勿在逻辑中调用 game:GetService()
  • 测试必须彼此独立;CI 需要模拟对象;提交 wally.lock 需要更多细节? 加载 references/full.md,查看包含代码示例、API 表和边缘情况的完整参考。

评论

支持 Markdown