我有一個SailsJS設置,並且正在使用Mocha/Supertest/Superagent組合來運行單元測試。我搜索了四周並閱讀了supertest,以及它如何擴展superagent.agent('url')以存儲會話和cookie。我知道我的/運動員/登錄和/運動員/當前路線正在工作,因爲我可以使用郵差測試他們,並且他們返回正確的值。不過,測試時,我得到一個登錄200個狀態,但上/運動員404 /電流使用SailsJS/Node,Mocha,Supertest進行身份驗證的會話存儲
這裏是我目前的工作:
1.摩卡測試登錄和驗證登錄的用戶會話在
var should = require('should'),
assert = require('assert'),
request = require('supertest');
it('/athlete/login should return 200 and athlete on success', function (done){
var athleteStub = AthleteStub(),
setPassword = athleteStub.password;
Athlete.create(athleteStub, function(err, newAthlete) {
var user = request.agent(sails.express.app);
user
.post('/athlete/login')
.send({ 'email': athleteStub.email, 'password': setPassword })
.expect('Content-Type', /json/)
.end(function (err, res) {
if(err) return done(err);
console.log('test', user);
// res.should.have.status(200);
// res.body.email.should.equal(athleteStub.email);
user
.get('/athlete/current')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
if(err) return done(err);
done();
});
});
});
});
2 /登錄和/當前的行動
login: function (req, res) {
var bcrypt = require('bcrypt');
Athlete.findOneByEmail(req.body.email).done(function (err, athlete) {
if (err) {
res.json({ error: 'DB error' }, 500);
}
if (athlete) {
if (!athlete.isActive){
res.json({ error: 'Your account is not active.' }, 500);
}
bcrypt.compare(req.body.password, athlete.password, function (err, match) {
if (err){
res.json({ error: 'Server error' }, 500);
}
if (match) {
// password match
req.session.athlete = athlete.id;
res.json(athlete);
} else {
// invalid password
if (req.session.athlete){
req.session.athlete = null;
}
res.json({ error: 'Invalid password' }, 403);
}
});
} else {
res.json({ error: 'User not found' }, 404);
}
});
},
current: function (req, res){
if(req.session.athlete){
Athlete.findOneById(req.session.athlete)
.where({ isActive: true })
.done(function(err, athlete) {
if (err) {
res.json({ error: 'No active athlete found with ID of '+req.params.id }, 404);
} else {
res.json(athlete);
}
});
}else{
res.json({ error: 'No active athlete currently logged in.' }, 404);
}
},
SOLUTION
我已經改變了一些路由所以基本上從上面把 '/運動員/電流/' 的現在是 '/運動員/ ME /'
it("/athlete/me should return user data if athlete is logged in", function(done){
var agent = request.agent(sails.hooks.http.app),
athleteStub = AthleteStub(),
setPassword = athleteStub.password;
agent
.post('/athlete')
.send(athleteStub)
.expect('Content-Type', /json/)
.end(function (err, res) {
should.not.exist(err);
res.should.have.status(200);
res.body.email.should.equal(athleteStub.email);
res.body.firstName.should.equal(athleteStub.firstName);
res.body.lastName.should.equal(athleteStub.lastName);
agent
.post('/athlete/login')
.send({ 'email': athleteStub.email, 'password': setPassword })
.expect('Content-Type', /json/)
.end(function (err, res) {
should.not.exist(err);
res.should.have.status(200);
agent
.get('/athlete/me')
.expect(200)
.end(function(err, res) {
should.not.exist(err);
res.body.email.should.equal(athleteStub.email);
res.should.have.status(200);
done();
});
});
});
});
嗨,有沒有找到一個解決您的問題?我面臨同樣的問題。提前致謝。 – jmcollin92
@ jmcollin92 - 沒有,仍然有同樣的問題。現在,我剛剛離開測試,以確保我的構建通過。我已經開始使用Sails的最新測試版了,並且最終會再次實現它(或者至少試圖) – cgaubuchon