Hapi Lab how to test all the required fields

三世轮回 提交于 2021-02-11 15:20:59

问题


Is there a way to check all the required fields without the need of a test each field.

validation rules

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

test

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

    });

});

回答1:


The answer from @simon-p-r would be a possible solution. But I do not understand why you want to validate the Joi Schemas by checking each required field with a test in the first place. As far as I can tell Joi has a test-coverage of 100% and can be considered thoroughly tested - so why do that again?

I would just test the success and failure case as well as some edge cases (like confirmation of password missing, wrong, etc.)...

'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...

});

I hope this helps.




回答2:


If you want to test all the field's validation you may want to set abortEarly to false in the options of validate method. If you are using built-in validation via route config set options object like so

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

This should catch all errors.



来源:https://stackoverflow.com/questions/33526978/hapi-lab-how-to-test-all-the-required-fields

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!