[英]mocha testing, chaining supertest requests
因此我試圖在我的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
做到這一點,所以我認為這是請求鏈接如何工作的問題。 如果需要更多上下文,我可以根據需要提供更多代碼。
解決方案就像更改一樣簡單
var req = require('supertest-as-promised')(app);
至
var req = require('supertest-as-promised').agent(app);
調用supertest.agent
可以使supertest
像一個Web會話一樣工作,並在鏈接請求時保留會話,cookie和標頭。
這是我使用超級測試代理的一些代碼;
/*
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.