因此,我正在尝试在我的express应用程序中测试一条路由,为此,我需要在打电话之前登录一个user。我在beforeEach函数中创建并保存了一个user。下面是我正在编写的测试:
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();
});
});
});下面是我运行测试时得到的输出:
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我搞不懂为什么它会在401中响应,而POST /login请求却以200响应。
使用Postman,我能够创建一个user,作为该用户登录,并且通过一个PUT请求,我能够成功地更新数据。因此,我假设这与supertest的req链接有关。
我已经使用supertest-as-promised和supertest编写了请求链。
据我所知,以下代码的行为与使用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();
});
});
});我被这里发生的事情搞糊涂了。就像我说的,我可以使用Postman来做这件事,所以我假设这是一个请求链工作方式的问题。如果您需要更多上下文,如果需要,我可以提供更多代码。
发布于 2016-01-04 20:28:42
解决方案很简单,只需更改
var req = require('supertest-as-promised')(app);
至
var req = require('supertest-as-promised').agent(app);
调用supertest.agent允许supertest作为web会话工作,并在链接请求时持久化会话、cookies和标头。
发布于 2016-01-04 19:40:17
下面是我使用supertest代理的一些代码;
/*
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()
})
})
})
})https://stackoverflow.com/questions/34589011
复制相似问题