0

有没有一种方法可以检查所有必填字段而无需测试每个字段。

验证规则

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();
        });

    });

});
4

2 回答 2

3

@simon-pr 的答案将是一个可能的解决方案。但我不明白您为什么要首先通过测试检查每个必填字段来验证 Joi 模式。据我所知,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...

});

我希望这有帮助。

于 2015-11-07T22:23:42.060 回答
1

如果您想测试所有字段的验证,您可能需要在 validate 方法的选项中将 abortEarly 设置为 false。如果您通过路由配置设置选项对象使用内置验证,如下所示

 {
    method: 'POST',
    path: '/api/users',
    config: {
        handler: handlerFunc,
        validate: {
            payload: Joi.schema(),
            options: {
                abortEarly: false
            }
        }
    }
},

这应该捕获所有错误。

于 2015-11-04T22:22:29.750 回答