我是摩卡和柴的新手。
我正在使用 mocha 和 chai 将集成测试用例编写到 NodeJS 应用程序中。
我一直在寻找一个通用的解决方案。
在这里,我正在解释我在做什么。
简而言之,我的观点是
如何为每个请求保留 cookie
我的节点服务器在端口 3000 上的单独机器上运行,测试用例将在另一台机器上。
这是我的应用程序目录结构
一、登录API测试用例
let chai = require('chai');
let chaiHttp = require('chai-http');
let expect = require('chai').expect;
chai.use(chaiHttp);
let agent = chai.request.agent(endpoint);
describe('/POST login ', function () {
it(' login ', function (done) {
let req = {
'email': 'email',
'password': 'password'
};
agent
.post(login)
.send(req)
.then(function (response) {
expect(response).to.have.status(200);
done();
}).catch(function (error) {
done(error);
});
});
});
二、获取账户列表
let chai = require('chai');
let chaiHttp = require("chai-http");
let expect = chai.expect;
chai.use(chaiHttp);
describe('/POST account', function () {
it(' Account List ', async function () {
let response = await chai.request(endpoint)
.post(accounts)
try {
expect(response).to.have.status(200);
} catch (error) {
throw error;
}
});
});
我为每个模块创建了单独的文件。
出色地,
所有 API 都基于会话进行身份验证。
所以,
有没有办法在 chai-http 插件上全局设置 cookie?
基本上,
登录测试用例会设置cookie,之后每个请求都会携带这个cookie。
谢谢