[英]mocha testing, chaining supertest requests
so I am trying to test a route in my express app, and in order to do so I need to login a user
before making my call. 因此我试图在我的Express应用中测试一条路线,为此,我需要在拨打电话之前登录
user
。 I create and save a user
in the beforeEach
function. 我在
beforeEach
函数中创建并保存一个user
。 Here is the test I am writing: 这是我正在编写的测试:
it('should update username', function(done){
var _this = this;
req.post('/login')
.send(_this.data)
.then(function(res){
req.put('/users/' + res.body.user._id)
.send({ username: 'robert'})
.expect(200)
.end(function(err, res){
if(err) return done(err);
console.log(res.body);
res.body.success.should.equal(true);
res.body.user.username.should.match(/robert/);
done();
});
});
});
Here is the output I get when I run the test: 这是运行测试时得到的输出:
Users
Routes
Authenticated
POST /login 200 195.059 ms - 142
PUT /users/568a432e1daa24083fa6778a 401 2.785 ms - 21
1) should update username
Unauthenticated
GET /users 401 1.502 ms - 21
✓ should return 401
1 passing (516ms)
1 failing
1) Users Routes Authenticated should update username:
Error: expected 200 "OK", got 401 "Unauthorized"
at Test._assertStatus (node_modules/supertest/lib/test.js:232:12)
at Test._assertFunction (node_modules/supertest/lib/test.js:247:11)
at Test.assert (node_modules/supertest/lib/test.js:148:18)
at Server.assert (node_modules/supertest/lib/test.js:127:12)
at net.js:1273:10
I'm confused why it's responding in a 401
, when the POST /login
request responded with a 200
. 我很困惑为什么它在
401
响应,而POST /login
请求以200
响应。
Using Postman
I am able to create a user
, login as that user, and with a PUT
request I am able to update the data successfuly. 使用
Postman
我可以创建一个user
,以该用户身份登录,并且通过PUT
请求,我可以成功更新数据。 So, I am assuming this has something to do with the req
chaining of supertest
. 因此,我假设这与
supertest
的req
链有关。
I have written the request chaining using both supertest-as-promised
as well as just supertest
. 我已经写了请求,利用链接都
supertest-as-promised
以及刚刚supertest
。
As far as I understand the following code behaves the same as using the then()
syntax: 据我了解,以下代码的行为与使用
then()
语法相同:
it('should update username', function(done){
var _this = this;
req.post('/login')
.send(_this.data)
.endfunction(err, res){
if(err) return done(err);
req.put('/users/' + res.body.user._id)
.send({ username: 'robert'})
.expect(200)
.end(function(err, res){
if(err) return done(err);
console.log(res.body);
res.body.success.should.equal(true);
res.body.user.username.should.match(/robert/);
done();
});
});
});
I'm confused by what is going on here. 我对这里发生的事情感到困惑。 Like I said, I can do this using
Postman
so I assume, this is a problem with how the request chaining is working. 就像我说的,我可以使用
Postman
做到这一点,所以我认为这是请求链接如何工作的问题。 If you need more context I can provide more code if need be. 如果需要更多上下文,我可以根据需要提供更多代码。
The solution was as simple as changing 解决方案就像更改一样简单
var req = require('supertest-as-promised')(app);
To 至
var req = require('supertest-as-promised').agent(app);
Calling supertest.agent
allows supertest
to work as a web session and persist sessions, cookies, and headers while chaining the requests. 调用
supertest.agent
可以使supertest
像一个Web会话一样工作,并在链接请求时保留会话,cookie和标头。
Here is some of my code using the supertest agent; 这是我使用超级测试代理的一些代码;
/*
Authentication tests
*/
process.env.NODE_ENV = 'test'
var should = require('should'),
app = require('../main.js'),
supertest = require('supertest')
describe('authentication', function(){
// I expose the webapp (express) on an object called app that is exported from main.js
var agent = supertest.agent(app.webapp)
before(function(cb){
// Create a user (I expose my models on an object called app)
var User = app.models.User
var adminUser = new User({
username : 'admin',
password : 'admin',
role : 'admin'
})
adminUser.save(function(err, _admin){
should.not.exist(err)
should.exist(_admin)
cb()
})
})
describe('invalid user', function(){
it('fail to login', function(cb){
agent.post('/api/v1/login').send({ username : 'NEONE', password : '123'}).end(function(err,res){
should(res.status).equal(401) // Unauthorised
cb()
})
})
it('is not logged in', function(done){
agent.get('/api/v1/loggedin').end(function(err, res){
res.body.should.equal('0')
done()
})
})
})
describe('valid user', function(){
it('should be able to login', function(done){
agent.post('/api/v1/login').send({ username : 'admin', password : 'admin'}).end(function(err,res){
should(res.status).equal(200) // Authorised
done()
})
})
it('should be logged in', function(done){
agent.get('/api/v1/loggedin').end(function(err, res){
should.not.exist(err)
res.status.should.equal(200)
res.body.username.should.equal('admin')
done()
})
})
it('should be able to logout', function(done){
agent.get('/api/v1/logout').end(function(err, res){
res.status.should.equal(200)
done()
})
})
it('should not be logged in', function(done){
agent.get('/api/v1/loggedin').end(function(err, res){
res.body.should.equal('0')
done()
})
})
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.