【发布时间】:2018-08-24 18:44:59
【问题描述】:
尽管我尽最大努力正确编写测试代码来验证 Setup 块或之前的 describe/it 块中的请求代理,但我在后续的 describe/it 块中从代理发出的任何请求都不会以 200 完成。
示例代码:
const request = require('supertest');
const server = require('../server');
let agent = request.agent(server);
let fakePerson = null;
beforeEach(async (done) => {
fakePerson = await Person.createMock();
agent.post(‘/login’)
.send({
email: ‘test@user.com’,
password: ‘password’
})
.end(function(err, res) {
if (err) throw err;
done();
});
});
describe('GET /users/:id', () => {
it ('renders user profile', () => {
return agent
.get(`/users/${fakePerson.id}`)
.expect(200)
});
});
我认为这可能与我在语法上形成异步调用的方式有关。但是在尝试使用return、.end() 语法,甚至异步/等待返回beforeEach 块中的登录调用的不同方法之后,我已经确定(即放弃)代码必须正确组合。会不会是别的?
参考文章/资源:
- How to authenticate Supertest requests with Passport?
- https://medium.com/@juha.a.hytonen/testing-authenticated-requests-with-supertest-325ccf47c2bb
- https://gist.github.com/joaoneto/5152248
- https://medium.com/@bill_broughton/testing-with-authenticated-routes-in-express-6fa9c4c335ca
- https://github.com/visionmedia/supertest/issues/46
软件包版本:
- “koa”:“^2.4.1”
- “koa-passport”:“^4.0.1”
- “passport-json”:“^1.2.0”
- “本地护照”:“^1.0.0”
- “超级测试”:“^3.0.0”
- “笑话”:“^22.1.3”
【问题讨论】:
-
在试图找出一个非常相似的问题时遇到了您的大部分文章/资源:stackoverflow.com/questions/60914997/…。请问你能解释一下吗?
-
@HarryLincoln 抱歉,从那以后我没有接触过这段代码或处理过类似的问题,所以我目前无法提供帮助。
-
感谢您一如既往地回复我@internetross!
标签: node.js jestjs supertest koa2