This skill guides you through test-driven development cycles, ensuring 80%+ coverage via red-green-refactor, with tests, code, and refactor steps.
npx playbooks add skill cacr92/wereply --skill tdd-workflow
强制要求:所有新功能、Bug 修复、重构必须达到 80% 以上测试覆盖率。
先写测试,验证测试会失败(因为功能尚未实现)。
编写最小代码使测试通过。
优化代码,保持所有测试通过。
作为用户,我希望能够创建新的饲料配方。
验收标准:
- 配方名称必填,长度 2-50 个字符
- 品种代码必填,必须是有效的品种
- 创建成功后返回配方 ID
Rust 测试:
#[tokio::test]
async fn test_create_formula_success() { }
#[tokio::test]
async fn test_create_formula_empty_name() { }
#[tokio::test]
async fn test_create_formula_name_too_long() { }
#[tokio::test]
async fn test_create_formula_invalid_species() { }
TypeScript 测试:
describe('FormulaForm', () => {
it('should create formula successfully', async () => { });
it('should show error when name is empty', async () => { });
it('should show error when name is too long', async () => { });
});
cargo test # Rust
npm test # TypeScript
预期结果:测试失败。
pub async fn create_formula(&self, dto: CreateFormulaDto) -> Result<i64> {
if dto.name.is_empty() {
return Err(anyhow!("配方名称不能为空"));
}
let formula_id = sqlx::query!(
"INSERT INTO formulas (name, species_code) VALUES (?, ?)",
dto.name, dto.species_code
)
.execute(&self.pool)
.await?
.last_insert_rowid();
Ok(formula_id)
}
cargo test
npm test
预期结果:所有测试通过。
// 提取验证逻辑
fn validate_formula_name(name: &str) -> Result<()> {
if name.is_empty() {
return Err(anyhow!("配方名称不能为空"));
}
Ok(())
}
pub async fn create_formula(&self, dto: CreateFormulaDto) -> Result<i64> {
validate_formula_name(&dto.name)?;
// ...
}
cargo tarpaulin --out Html # Rust
npm run test:coverage # TypeScript
要求:覆盖率 >= 80%。
#[test]
fn test_validate_formula_name() {
assert!(validate_formula_name("测试配方").is_ok());
assert!(validate_formula_name("").is_err());
}
#[tokio::test]
async fn test_create_formula_integration() {
let pool = setup_test_db().await;
let repo = FormulaRepository::new(Arc::new(pool));
let dto = CreateFormulaDto {
name: "测试配方".to_string(),
species_code: "PIG".to_string(),
};
let result = repo.create(dto).await;
assert!(result.is_ok());
cleanup_test_db().await;
}
import { vi } from 'vitest';
vi.mock('../bindings', () => ({
commands: {
createFormula: vi.fn(),
},
}));
it('should create formula', async () => {
vi.mocked(commands.createFormula).mockResolvedValue({
success: true,
data: { id: 1 },
});
const result = await createFormula(dto);
expect(result.success).toBe(true);
});
use mockall::mock;
mock! {
pub FormulaRepository {
async fn create(&self, dto: CreateFormulaDto) -> Result<i64>;
}
}
#[tokio::test]
async fn test_with_mock() {
let mut mock_repo = MockFormulaRepository::new();
mock_repo.expect_create().returning(|_| Ok(1));
let service = FormulaService::new(Arc::new(mock_repo));
let result = service.create_formula(dto).await;
assert!(result.is_ok());
}
test_validate_formula_name_rejects_empty_stringWeekly roundup of top Claude Code skills, MCP servers, and AI coding tips.