Skill站外收录 @skilllib/skill-2117939930272417696
wheels-test-generator
生成适用于 Wheels 框架的 BDD 测试规范,覆盖模型验证、关联关系、控制器动作及集成工作流,确保测试完整性。 支持单元与端到端测试,结合浏览器自动化工具模拟用户操作,保障应用逻辑与数据库交互正确性。
测试与安全 AI 技能
Skills.MD
# Wheels Test Generator
## When to Use This Skill
Activate automatically when:
- User requests to create tests/specs
- User wants to test a model, controller, or workflow
- User mentions: test, spec, TestBox, BDD, describe, it, expect
- After generating models/controllers (proactive testing)
## Model Test Template
```cfm
component extends="wheels.Test" {
function setup() {
// Runs before each test
super.setup();
model = model("Post").new();
}
function teardown() {
// Runs after each test
if (isObject(model) && model.isPersisted()) {
model.delete();
}
super.teardown();
}
function testValidatesPresenceOfTitle() {
model.title = "";
assert("!model.valid()");
assert("model.hasErrors('title')");
}
function testHasManyComments() {
model = model("Post").create(title="Test", content="Content");
comment = model("Comment").create(postId=model.id, content="Comment");
assert("model.comments().recordCount == 1");
model.delete(); // Cascade should delete comment
assert("!isObject(model('Comment').findByKey(comment.id))");
}
}
```
## Controller Test Template
```cfm
component extends="wheels.Test" {
function setup() {
super.setup();
params = {controller="posts", action="index"};
}
function testIndexLoadsAllPosts() {
controller = controller("Posts", params);
controller.processAction("index");
assert("isQuery(controller.posts)");
}
function testShowRequiresKey() {
params.action = "show";
controller = controller("Posts", params);
// Should redirect due to missing key
}
function testCreateWithValidData() {
params.action = "create";
params.post = {title="Test", content="Content"};
controller = controller("Posts", params);
controller.processAction("create");
assert("flashKeyExists('success')");
}
}
```
## Integration Test Template
```cfm
component extends="wheels.Test" {
function testCompletePostLifecycle() {
// Create
post = model("Post").create(title="Test", content="Content");
assert("isObject(post) && post.isPersisted()");
// Update
post.update(title="Updated");
assert("post.title == 'Updated'");
// Add comment
comment = model("Comment").create(postId=post.id, content="Comment");
assert("post.comments().recordCount == 1");
// Delete (cascade)
post.delete();
assert("!isObject(model('Comment').findByKey(comment.id))");
}
}
```
## Browser-Based Integration Testing
For end-to-end testing of user workflows, use MCP browser tools:
### Basic Browser Test Flow
```javascript
// 1. Start server and navigate
mcp__wheels__wheels_server(action="status")
// 2. Use available browser MCP (in order of preference):
// Option A: Puppeteer MCP (preferred)
mcp__puppeteer__puppeteer_navigate(url="http://localhost:PORT/posts")
mcp__puppeteer__puppeteer_screenshot(name="posts-index")
mcp__puppeteer__puppeteer_click(selector="a[href*='new']")
mcp__puppeteer__puppeteer_screenshot(name="posts-new-form")
// Option B: Playwright MCP
mcp__playwright__playwright_navigate(url="http://localhost:PORT/posts")
mcp__playwright__playwright_screenshot()
// Option C: Browser MCP (fallback)
mcp__browsermcp__browser_navigate(url="http://localhost:PORT/posts")
mcp__browsermcp__browser_screenshot()
```
### Complete User Workflow Test
```javascript
// Test: Create new post
// 1. Navigate to new post form
mcp__puppeteer__puppeteer_navigate(url="http://localhost:PORT/posts/new")
mcp__puppeteer__puppeteer_screenshot(name="step1-form")
// 2. Fill form fields
mcp__puppeteer__puppeteer_click(selector="input[name='post[title]']")
// Enter data...
// 3. Submit and verify
mcp__puppeteer__puppeteer_click(selector="button[type='submit']")
mcp__puppeteer__puppeteer_screenshot(name="step2-created")
// 4. Verify success message or redirect
// Check screenshot for success indicators
```
### Testing CRUD Operations
```javascript
// CREATE: Test form submission
mcp__puppeteer__puppeteer_navigate(url="http://localhost:PORT/posts/new")
// READ: Test index and show pages
mcp__puppeteer__puppeteer_navigate(url="http://localhost:PORT/posts")
mcp__puppeteer__puppeteer_navigate(url="http://localhost:PORT/posts/1")
// UPDATE: Test edit form
mcp__puppeteer__puppeteer_navigate(url="http://localhost:PORT/posts/1/edit")
// DELETE: Test delete action
// Verify through index page
```
## 🚨 Test Database Setup (CRITICAL)
### Issue: Test Environment Migrations
**Problem:** Test environment uses `tests/populate.cfm` which may fail to run migrations properly, falling back to manual table creation.
**Symptoms:**
- Tests fail with "table not found" errors
- Tests fail with "no primary key" errors
- Migrations work in development but not in test environment
- populate.cfm catches migrator errors and creates tables manually
**Root Cause:** Test environment `populate.cfm` tries to run migrations via Migrator component but catches errors and falls back to manual SQL, which may not match migration schema exactly.
### Solution: Update populate.cfm for New Tables
When adding new tables/migrations, you MUST update `tests/populate.cfm` to include manual table creation:
```cfm
<cfscript>
// Run Wheels migrations to set up test database schema
try {
migrator = createObject("component", "wheels.migrator.Migrator").init(
migratePath = application.wo.get("rootPath") & "app/migrator/migrations/",
datasourceName = application.wheels.dataSourceName
);
migrator.migrateToVersion();
} catch (any e) {
writeLog(file="application", text="Error running migrations: #e.message#");
// Fallback to manual table creation
try {
// Create each table manually with H2-compatible syntax
queryExecute("DROP TABLE IF EXISTS tablename", {}, {datasource: application.wheels.dataSourceName});
queryExecute("
CREATE TABLE tablename (
id INT IDENTITY PRIMARY KEY, -- H2 uses IDENTITY not AUTO_INCREMENT
columnName VARCHAR(255) NOT NULL,
createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
deletedAt TIMESTAMP
)
", {}, {datasource: application.wheels.dataSourceName});
// Add indexes
queryExecute("CREATE INDEX idx_tablename_column ON tablename(columnName)",
{}, {datasource: application.wheels.dataSourceName});
} catch (any e2) {
writeLog(file="application", text="Error creating tables manually: #e2.message#");
}
}
</cfscript>
```
### H2 Database Syntax (CRITICAL)
**When writing manual table creation for tests, use H2-compatible syntax:**
```cfm
// ✅ CORRECT for H2:
id INT IDENTITY PRIMARY KEY
// ❌ WRONG - causes "no primary key" errors:
id INT AUTO_INCREMENT PRIMARY KEY
id INTEGER AUTO_INCREMENT PRIMARY KEY
```
**Key H2 Differences:**
- Use `IDENTITY` not `AUTO_INCREMENT` for auto-increment columns
- Use `INT` not `INTEGER` for integer columns
- Use `VARCHAR(n)` not `VARCHAR2(n)`
- Use `TIMESTAMP` not `DATETIME`
### Test Database Workflow
**Option A: Manual Table Creation (Current)**
1. Update `tests/populate.cfm` with manual CREATE TABLE statements
2. Use H2-compatible syntax (IDENTITY for primary keys)
3. Match migration schema exactly
4. Include all indexes and constraints
**Option B: Fix Migrator Integration (Future)**
1. Debug why migrator fails in test environment
2. Fix migration system to work with test database
3. Remove manual table creation fallback
4. Let migrations handle test database schema
### Checklist for New Tables
When adding new models/migrations:
- [ ] Create migration in `app/migrator/migrations/`
- [ ] Run migration in development: `wheels dbmigrate latest`
- [ ] Update `tests/populate.cfm` with manual H2 table creation
- [ ] Use `IDENTITY` for primary keys in test tables
- [ ] Match column names/types exactly between migration and populate.cfm
- [ ] Run tests to verify: `wheels test run`
## Related Skills
- **wheels-model-generator**: Creates models to test
- **wheels-controller-generator**: Creates controllers to test
- **wheels-debugging**: Use when tests fail
---
**Generated by:** Wheels Test Generator Skill v1.1
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「wheels-test-generator」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次