字符串断言完整参考 - equal include string match lengthOf ignoreCase
完整断言方法一览
| 断言 | 说明 | 示例 |
|---|---|---|
.equal(str) | 精确值比较(严格) | expect('hello').to.equal('hello') |
.deep.equal | 对字符串没意义 | — |
.include(substr) | 包含子串 | expect('hello').to.include('ell') |
.string(str) | 值等于(.equal 的别名?不是——见下) | — |
.match(regex) | 正则匹配 | expect('h123').to.match(/^\w+\d+$/) |
.lengthOf(n) | 长度 | expect('hello').to.have.lengthOf(5) |
.above(n).length() | 长度大于 | expect('hello').to.have.length.above(3) |
.below(n).length() | 长度小于 | expect('hello').to.have.length.below(10) |
.within(n1, n2).length() | 长度区间 | expect('hello').to.have.length.within(3, 7) |
.empty | 空字符串 | expect('').to.be.empty |
.a('string') | 类型检查 | expect('hello').to.be.a('string') |
.exist / .not.exist | 存在性 | expect(str).to.exist |
.ignoreCase | 忽略大小写修饰 | expect('HELLO').to.ignoreCase.equal('hello') |
.includeSpace / .includeSpaces | .string() 检查 | — |
.startsWith / .startWith | 前缀匹配(需插件) | — |
.endsWith / .endWith | 后缀匹配(需插件) | — |
.equal —— 精确值
expect('hello').to.equal('hello'); // ✅
expect('hello').to.equal('Hello'); // ❌ 大小写敏感
expect('hello').to.not.equal('world'); // ✅
.include —— 包含子串
expect('hello world').to.include('world'); // ✅
expect('hello world').to.include('hello'); // ✅
expect('hello').to.include('xyz'); // ❌
.include 也可以用于数组(包含元素)和对象(包含属性),语义一致。
配合 .not 和 .ignoreCase:
expect('Hello World').to.ignoreCase.include('world'); // ✅
expect('abc').to.not.include('d'); // ✅
.lengthOf —— 长度
expect('hello').to.have.lengthOf(5);
expect('').to.have.lengthOf(0);
可以和 .above / .below / .within 组合:
expect('hello').to.have.length.above(3); // 长度 > 3
expect('hello').to.have.length.below(10); // 长度 < 10
expect('hello').to.have.length.within(4, 6); // 长度 ∈ [4, 6]
注意这里的 .length 是一个语言链(修饰),不是实际断言。实际断言是 .above / .below / .within。
.string(str) —— 特别说明
.string(str) 检查目标是否精确等于某个字符串值,类似 .equal 但有一个额外的修饰器 .includeSpace 系列。
expect('hello').to.equal('hello'); // 和 .equal 行为相同
除非你需要 .includeSpace / .includeSpaces / .ignoreSpaces 修饰器,否则直接用 .equal 就好。
.ignoreCase —— 忽略大小写修饰器
链式中插入 .ignoreCase,后续的字符串比较不区分大小写:
expect('HELLO').to.ignoreCase.equal('hello');
expect('Hello World').to.ignoreCase.include('WORLD');
expect('hElLo').to.ignoreCase.match(/^hello$/);
只影响 .string、.equal、.include、.match 等字符串断言。
.empty —— 空字符串
expect('').to.be.empty;
expect('hello').to.not.be.empty;
.empty 也可用于数组和对象:
expect([]).to.be.emptyexpect({}).to.be.empty
前缀 / 后缀匹配(需插件)
Chai 核心不提供 .startsWith / .endsWith。安装 chai-string 插件:
import 'chai/register-should';
import chaiString from 'chai-string';
chai.use(chaiString);
'hello'.should.startWith('hel');
'hello'.should.endWith('llo');
常见字符串场景
URL slug 验证
const slug = 'my-great-post-2024';
expect(slug).to.match(/^[a-z0-9-]+$/); // 只允许小写字母、数字、短横线
expect(slug).to.have.length.within(5, 80);
CSV / TSV 解析验证
const line = 'Alice,30,Engineering';
expect(line).to.include(','); // 至少一个分隔符
// 拆分数组后逐字段验证
const fields = line.split(',');
expect(fields).to.have.lengthOf(3);
expect(fields[0]).to.not.be.empty;
expect(fields[1]).to.match(/^\d+$/);
HTML 输入净化
// 输入不应该包含 XSS 脚本标签
expect(userInput).to.not.match(/<script/);
expect(userInput).to.not.match(/on\w+=/);
assert 风格字符串方法
assert.isString(value);
assert.lengthOf(str, n);
assert.include(str, sub);
assert.notInclude(str, sub);
assert.match(str, regex);