【问题标题】:Hapi Lab how to test all the required fieldsHapi Lab 如何测试所有必填字段
【发布时间】:2015-11-08 06:23:42
【问题描述】:

有没有办法检查所有必填字段 无需测试每个字段。

验证规则

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

    });

});

【问题讨论】:

  • 你想做什么?你在测试 joi 是否验证成功?
  • 我正在测试必填字段,并且正在寻找一种方法来避免对每个字段进行大量琐碎的测试。有没有办法在一次测试中测试所有必填字段?

标签: hapijs lab


【解决方案1】:

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

});

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

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

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

    这应该会捕获所有错误。

    【讨论】:

    • 感谢您的提示,但我不想更改逻辑进行测试
    猜你喜欢
    • 1970-01-01
    • 2021-04-18
    • 2014-08-07
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    相关资源
    最近更新 更多