gpt4 book ai didi

javascript - Hapi.js 实验室测试结果不一致

转载 作者:行者123 更新时间:2023-12-03 07:37:38 27 4
gpt4 key购买 nike

我正在尝试测试具有注册功能的 Hapi.js 插件:

exports.register = function(server, options, next) {

server.route({
method: 'POST',
path: '/register',
config: {
payload: {
allow: 'application/json'
},
validate: {
/* deleted for brevity */
}
},

handler: function(request, reply) {

if (!server.app.mongoose) {

server.log('error', 'Failed to find an active MongoDB connection.');

return reply(Boom.badImplementation());
}

var response = reply().hold();

var mongoose = server.app.mongoose;

var User = mongoose.model('User');

var password = request.payload.password;

return new Promise(function(resolve, reject) {

bcrypt.genSalt(10, function(err, salt) {

if (err) {

server.log('error', 'Failed to generate bcrypt salt: ' + err);

return reject();
}

bcrypt.hash(password, salt, function(err, hash) {

/* deleted for brevity */

user.save(function(err, savedUser) {

if (err) {

server.log('error', 'Failed to save user to the database: ' + err);

return reject(Boom.conflict());
}

server.log('debug', 'Registered new user with e-mail validation code: ' + validationCode);

resolve({});
});
});
});

}).then(function(data) {

response.statusCode = 201;
response.source = data;
response.send();

return response;

}, function(err) {

if (!err)
err = Boom.badImplementation();

response.statusCode = err.output.statusCode;
response.source = err.output.payload;
response.send();

return response;
});
}
});

next();
};

我的测试文件在这里:

const Lab = require('lab');
const expect = require('code').expect;

const server = require('../');
const lab = exports.lab = Lab.script();

const mongoose = require('../plugins/mongo.js').mongoose;

lab.experiment('Registration', function() {

lab.before(function(done) {

mongoose.connection.collections['users'].drop(function(err, resp) {

if (err) {

console.error(err);

} else {

console.log(resp);
}
});

var User = mongoose.model('User');

/* deleted for brevity */

user.save(function(err, savedUser) {

if (err) {

server.log('error', 'Failed to save user to the database: ' + err);

done(err);
}

done();
});
});

lab.test('/register endpoint with empty payload', function(done) {

server.inject({
method: 'POST',
url: '/register',
payload: {}
}, function(response) {

expect(response.statusCode).to.be.equal(400);
expect(response.result.message).to.match(/^child "\w+" fails because \["\w+" is required\]$/);

done();
});
});

lab.test('/register endpoint with invalid email', function(done) {

server.inject({
method: 'POST',
url: '/register',
payload: {
...
}
}, function(response) {

expect(response.statusCode).to.be.equal(400);
expect(response.result.message).to.be.equal('child "email" fails because ["email" must be a valid email]');

done();
});
});

lab.test('/register endpoint with short password', function(done) {

server.inject({
method: 'POST',
url: '/register',
payload: {
...
}
}, function(response) {

expect(response.statusCode).to.equal(400);
expect(response.result.message).to.startWith('child "password" fails because ["password" length must be at least');

done();
});
});

lab.test('/register endpoint with invalid password', function(done) {

server.inject({
method: 'POST',
url: '/register',
payload: {
...
}
}, function(response) {

expect(response.statusCode).to.equal(400);
expect(response.result.message).to.startWith('child "password" fails because');

done();
});
});

lab.test('/register endpoint with existing username', function(done) {

server.inject({
method: 'POST',
url: '/register',
payload: {
...
}
}, function(response) {

expect(response.statusCode).to.equal(409);
done();
});
});

lab.test('/register endpoint with valid payload', function(done) {

server.inject({
method: 'POST',
url: '/register',
payload: {
...
}
}, function(response) {

expect(response.statusCode).to.equal(201);

done();
});
});
});

一切都很好,直到我添加“/使用现有用户名注册端点”。

现在,实验室命令的后续调用成功退出,然后测试 5 和 6 失败(就像这样继续下去,一次成功,然后一次失败)。基本上,看起来成功的测试启动后结果状态代码被交换。

看起来像是同步问题,但我找不到原因所在。有什么想法吗?

最佳答案

before步骤中,将save移动到drop回调中。

在您当前的代码中,save 有可能会在 drop 之前完成。因此用户可能不存在,这将导致测试 5 和 6 交替失败和成功

关于javascript - Hapi.js 实验室测试结果不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35544304/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com