Hapi Lab如何测试所有必填字段 [英] Hapi Lab how to test all the required fields

查看:74
本文介绍了Hapi Lab如何测试所有必填字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

    });

});

推荐答案

@ simon-p-r的回答可能是一种解决方案.但是我不明白为什么您要通过首先通过测试检查每个必填字段来验证Joi模式.据我所知,Joi的测试覆盖率是100%,可以被认为是经过全面测试的-那么为什么还要再次测试呢?

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

});

我希望这会有所帮助.

这篇关于Hapi Lab如何测试所有必填字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆