如何通过 mocha 和 chai 匹配网页上呈现的特定字符串
How to match a particular string rendered on the webpage through mocha and chai
我正在使用 mocha 和 chai 包在我的 Node JS 应用程序中构建测试套件。截至目前,我正在通过检查成功呈现页面时返回的状态代码 (200) 来将测试用例验证为 pass/fail。
如何根据我在页面上呈现的内容将页面验证为 pass/fail。例如,如果页面显示 "Welcome to Express" ,我想匹配任何一个单词 "Welcome" 或 "Express" 以将其验证为通过。
下面是我检查状态码的代码片段:
describe('Home Page', () => {
it('This is the Home page', (done)=> {
chai.request(server)
.get('/home')
.end((error, respond) => {
expect(respond.statusCode).to.equal(200);
done();
});
});
});
作为一个简单的解决方案,您可以直接在 expect() 语句中使用要匹配的 content/string,如下所示:
describe('Home Page', () => {
it('This is the Home page', (done)=> {
chai.request(server)
.get('/home')
.end((error, respond) => {
expect(/Welcome/, done);
done();
});
});
});
我正在使用 mocha 和 chai 包在我的 Node JS 应用程序中构建测试套件。截至目前,我正在通过检查成功呈现页面时返回的状态代码 (200) 来将测试用例验证为 pass/fail。
如何根据我在页面上呈现的内容将页面验证为 pass/fail。例如,如果页面显示 "Welcome to Express" ,我想匹配任何一个单词 "Welcome" 或 "Express" 以将其验证为通过。
下面是我检查状态码的代码片段:
describe('Home Page', () => {
it('This is the Home page', (done)=> {
chai.request(server)
.get('/home')
.end((error, respond) => {
expect(respond.statusCode).to.equal(200);
done();
});
});
});
作为一个简单的解决方案,您可以直接在 expect() 语句中使用要匹配的 content/string,如下所示:
describe('Home Page', () => {
it('This is the Home page', (done)=> {
chai.request(server)
.get('/home')
.end((error, respond) => {
expect(/Welcome/, done);
done();
});
});
});