Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

egg test 返回的信息和egg-validate 返回的信息不一致 #3157

Closed
gogogo1024 opened this issue Nov 4, 2018 · 8 comments
Closed

egg test 返回的信息和egg-validate 返回的信息不一致 #3157

gogogo1024 opened this issue Nov 4, 2018 · 8 comments
Assignees

Comments

@gogogo1024
Copy link

gogogo1024 commented Nov 4, 2018

  • Node Version:v8.7.0
  • Egg Version:v2.12.0
  • Plugin Name:egg-validate
  • Plugin Version:2.0.2
  • Platform:macos
  • Mini Showcase Repository:

在对controller中的文件写测试用例的时候,对于常见比如说登录这一块,比如验证邮箱没填写,egg-validate 对其进行校验时,返回的状态码是422,这个和在测试文件的结果一致,但是返回的数据两者有差异,我贴个截图吧
2018-11-04 4 00 30

@egg-bot
Copy link

egg-bot commented Nov 5, 2018

Hello @jxycbjhc. Please provide a reproducible example by creating a github repo.

Issues labeled by Need Reproduce will be closed if no activities in 7 days.

@gogogo1024
Copy link
Author

app/controller/user.js

 const paramRule = {
      email: {
        type: 'string',
        required: true
      },
      password: {
        type: 'string',
        required: true
      },
    };
    // 校验参数email和密码
    let paramObj = {
      email: email,
      password: password,
    };
    const paramErrors = this.app.validator.validate(paramRule, paramObj);
    if (!_.isArray(emailErrors)) {
        //...
    }else{
          ctx.body = paramErrors;
          ctx.status = 422;
    }

test/controller/user.test.js

const { app } = require('egg-mock/bootstrap');
describe('login()', () => {
    it('without email should status 422 and get the response body', () => {
        return app.httpRequest()
            .post('/user/login')
            .type('form')
            .send({
                // email: '[email protected]',
                password: '123456',
            })
            .expect(422)
            .expect([{
                "message": "should not be empty",
                "code": "invalid",
                "field": "email"
            }]);
    });
     it('without password should status 422 and get the response body', () => {
        return app.httpRequest()
            .post('/user/login')
            .type('form')
            .send({
                email: '[email protected]',
               //password: '123456',
            })
            .expect(422)
            .expect([{
                "message": "should not be empty",
                "code": "invalid",
                "field": "password"
            }]);
    });
});

@atian25
Copy link
Member

atian25 commented Nov 6, 2018

egg-init --type=simple 初始化个最小可复现仓库,然后提交到你的 GitHub 吧

@gogogo1024
Copy link
Author

gogogo1024 commented Nov 11, 2018

这个应该是能反映问题的 [email protected]:jxycbjhc/egg-issue.git

@thonatos
Copy link
Member

看了一下,测试用例好像不对?参考下面的测试用例。

@gogogo1024
Copy link
Author

@thonatos 不是太明白,这个是对egg-validate模块的测试文件,我想表达的是egg-validate模块提供的报错和在测试文件下的报错是不一致的。

@thonatos
Copy link
Member

@jxycbjhc

跑了你提供的代码,结果也确实和截图一样。

但是问题是,此处并没有按照 egg-validate 文档使用,使用问题请参考egg-validate文档。

@gogogo1024
Copy link
Author

gogogo1024 commented Nov 15, 2018

好的,看了下其他的issue ,#1086 看来是egg-onerror统一处理了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants