有没有办法检查所有必填字段 无需每个领域的测试。
const Confirmation = Joi.any().valid(Joi.ref('password')).required().options({ language: { any: { allowOnly: 'must match password' } } });
const Email = Joi.string().email();
const Firstname = Joi.string().regex(/^[a-zA-Z\']+$/).min(2).max(30);
const Lastname = Joi.string().regex(/^[a-zA-Z\']+$/).min(2).max(30);
const Password = Joi.string().min(3).max(30);
const Username = Joi.string().regex(/^[a-zA-Z\-\.]+$/).min(3).max(30);
exports.create = {
payload: {
email: Email.required(),
firstname: Firstname.required(),
lastname: Lastname.required(),
password: Password.required(),
password_confirmation: Confirmation,
username: Username.required()
}
};
'use strict';
const Lab = require('lab');
const lab = exports.lab = Lab.script();
const Code = require('code');
const Server = require('../../index');
lab.experiment('User', function() {
lab.test('create firstname should be required', function (done) {
const options = {
method: 'POST',
url: '/api/users',
payload: {
email: 'me@mydomain.com',
password: 'mysecret'
}
};
Server.inject(options, function(response) {
const result = response.result;
Code.expect(response.statusCode).to.equal(422);
Code.expect(result.message).to.equal('child "firstname" fails because ["firstname" is required]');
done();
});
});
//AND SO ON
lab.test('create firstname should be required', function (done) {
const options = {
method: 'POST',
url: '/api/users',
payload: {
email: 'me@mydomain.com',
password: 'mysecret',
firstname: 'me'
}
};
Server.inject(options, function(response) {
const result = response.result;
Code.expect(response.statusCode).to.equal(422);
Code.expect(result.message).to.equal('child "lastname" fails because ["lastname" is required]');
done();
});
});
});
答案 0 :(得分:3)
@ simon-p-r的答案将是一个可能的解决方案。但我不明白为什么要通过首先用测试检查每个必需字段来验证Joi Schemas。据我所知,Joi的测试覆盖率为100%,可以考虑进行全面测试 - 为什么要再次测试呢?
我只是测试成功和失败的情况以及一些边缘情况(如确认密码缺失,错误等)......
'use strict';
const Lab = require('lab');
const lab = exports.lab = Lab.script();
const Code = require('code');
const Server = require('../../index');
lab.experiment('User', function() {
//Failure case
lab.test('create should fail if required fields are missing', function(done) {
const options = {
method: 'POST',
url: '/api/users',
payload: {
email: 'me@mydomain.com',
firstname: 'foo',
lastname: 'bar'
}
};
Server.inject(options, function(response) {
Code.expect(response.statusCode).to.equal(400);
done();
});
});
//Success case
lab.test('create should succeed if all fields are valid', function(done) {
const options = {
method: 'POST',
url: '/api/users',
payload: {
email: 'me@mydomain.com',
firstname: 'foo',
lastname: 'bar',
password: 'secret',
password_confirmation: 'secret',
username: 'foo.bar'
}
};
Server.inject(options, function(response) {
Code.expect(response.statusCode).to.equal(200);
//maybe do some more checks on the response here...
done();
});
});
//Edge cases
lab.test('create should succeed if all fields are valid', function(done) {
const options = {
method: 'POST',
url: '/api/users',
payload: {
email: 'me@mydomain.com',
firstname: 'foo',
lastname: 'bar',
password: 'secret',
password_confirmation: 'something_else',
username: 'foo.bar'
}
};
Server.inject(options, function(response) {
Code.expect(response.statusCode).to.equal(400);
//maybe do some more checks on the response here...
done();
});
});
//And so on...
});
我希望这会有所帮助。
答案 1 :(得分:1)
如果要测试所有字段的验证,可能需要在validate方法的选项中将abortEarly设置为false。如果您通过路由配置集选项对象使用内置验证,那么
{
method: 'POST',
path: '/api/users',
config: {
handler: handlerFunc,
validate: {
payload: Joi.schema(),
options: {
abortEarly: false
}
}
}
},
这应该可以捕获所有错误。