roblox-testing

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

When to Load

加载时机

Load when writing unit/integration tests, mocking Roblox services, setting up CI/CD with Lune, or refactoring code for testability via dependency injection. Hand off to
roblox-luau-core
for syntax,
roblox-luau-types
for type design,
roblox-luau-patterns
for architecture,
roblox-security
for validation.
在编写单元/集成测试、模拟Roblox服务、基于Lune搭建CI/CD流程,或通过依赖注入重构代码以提升可测试性时加载。语法相关内容请参考
roblox-luau-core
,类型设计参考
roblox-luau-types
,架构参考
roblox-luau-patterns
,验证相关参考
roblox-security

Quick Reference

快速参考

TestEZ (BDD framework)

TestEZ(BDD框架)

  • Install:
    [dev-dependencies] TestEZ = "roblox/testez@0.4.1"
    in
    wally.toml
  • Files:
    *.spec.luau
    , co-located with source
  • Blocks:
    describe
    it
    beforeEach
    /
    afterEach
  • Assertions:
    expect(x).to.equal(y)
    ,
    .to.be.near(n, ε)
    ,
    .to.throw()
  • 安装:在
    wally.toml
    中添加
    [dev-dependencies] TestEZ = "roblox/testez@0.4.1"
  • 文件:
    *.spec.luau
    ,与源代码同目录存放
  • 代码块结构:
    describe
    it
    beforeEach
    /
    afterEach
  • 断言:
    expect(x).to.equal(y)
    .to.be.near(n, ε)
    .to.throw()

Dependency Injection

依赖注入

luau
-- 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
luau
-- 不良示例:无法在Studio外测试
local store = game:GetService("DataStoreService"):GetDataStore("PlayerData")
-- 良好示例:注入依赖项
local function saveData(player, data, dataStore)
    dataStore:SetAsync("player_" .. player.UserId, data)
end
-- 模块初始化模式:
function Manager.init(dataStoreService)
    _dss = dataStoreService or game:GetService("DataStoreService")
end

Mocking Essentials

模拟基础

luau
-- 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
luau
-- MockDataStore:内存版的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及Connected状态标记

CI/CD with Lune

基于Lune的CI/CD

toml
undefined
toml
undefined

aftman.toml

aftman.toml

[tools] lune = "lune-org/lune@0.8.0"

Pipeline: `selene src/` → `stylua --check src/` → `luau-lsp analyze` → `lune run tests/run.luau`

```lua
-- 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
[tools] lune = "lune-org/lune@0.8.0"

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

```lua
-- 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

Testing Priority

测试优先级

  1. DataStore save/load round-trips (prevents data loss)
  2. Monetization ProcessReceipt (idempotency)
  3. Core math (damage, buffs, economy)
  4. Server validation (remote exploits)
  1. DataStore保存/加载往返测试(防止数据丢失)
  2. 变现流程ProcessReceipt测试(幂等性)
  3. 核心计算逻辑测试(伤害、增益、经济系统)
  4. 服务器验证测试(远程漏洞防护)

Pitfalls

常见陷阱

  • beforeEach
    to reset state; never
    game:GetService()
    in logic
  • Tests must be order-independent; CI needs mocks; commit
    wally.lock
    Need more detail? Load
    references/full.md
    for the complete reference with code examples, API tables, and edge cases.
  • 使用
    beforeEach
    重置状态;切勿在业务逻辑中直接调用
    game:GetService()
  • 测试必须独立于执行顺序;CI环境需要使用模拟对象;提交
    wally.lock
    文件 需要更多细节? 请查看
    references/full.md
    获取包含代码示例、API表格及边界情况的完整参考文档。