【问题标题】:Stuck while testing authentication using nock and mocha使用 nock 和 mocha 测试身份验证时卡住
【发布时间】:2019-02-17 13:20:06
【问题描述】:

我正在为进行第三方 API 调用的应用编写单元测试。我正在使用 nock 来模拟 API 调用。 API 使用用户名和密码进行基本身份验证。

这是一个单元测试来验证用户,然后返回响应。

describe('Authentication tests', () => {

    it('Authenticates user', (done) => {

        nock('<MY_URL>')
            .get('/api/now/table/test_table/test_sys_id')
            .basicAuth({user: 'user', pass: 'pass'})
            .reply(200, {'a': 'b'});

        let snowClient = require('../Client');
        snowClient = new snowClient('<MY_URL>', 'user', 'pass', 'application/json', 'application/json');      

        snowClient.getSingleRecord('test_table', 'test_sys_id', (response) => {
            console.log(response);
            expect(typeof response).to.be.a('string');
            // expect(JSON.parse(response).a).to.equal('b');
            done();
        });
    });
});

但是当我运行这个测试时,nock 会抛出以下错误:

Error: Nock: No match for request {
  "method": "GET",
  "url": "<MY_URL>/api/now/table/test_table/test_sys_id",
  "headers": {
    "accept": "application/json",
    "content-type": "application/json",
    "host": "<MY_URL>"
  }
}

但是当我从 nock 中删除 .basicAuth() 时,它可以正常工作。我在这里想念什么?这是使用 nock 测试经过身份验证的 API 调用的正确方法吗?

编辑:

这里是getSingleRecord的代码:

getSingleRecord(table, sysId, callback){
    new Promise((resolve, reject) => {
        var requestParams = utils.setRequestParamsWithoutBody(this.headers, 
                                                                this.auth,
                                                                'GET', 
                                                                utils.URLBuilder(this.instance, this.namespace, this.apiName, table, sysId));

        request(requestParams, (err, res) => {
            var errorMessage = this._handleErrors(res, err);
            if (errorMessage){
                return reject(errorMessage);
            }
            resolve(res.body);
        });
    }).then((response) => {
        callback(response);
    }).catch((error) => {
        callback(error);
    });
}

【问题讨论】:

    标签: javascript node.js unit-testing mocha.js nock


    【解决方案1】:

    我认为你不应该在it中使用nock,应该在describe的范围内使用它,我没有测试它,我只是研究它,认为它应该像这样工作。

    describe('Authentication tests', () => {
    let MY_URL = 'you url'
    
      beforeEach(() => {
        nock(MY_URL)
              .get('/api/now/table/test_table/test_sys_id')
              .basicAuth({user: 'user', pass: 'pass'})
              .reply(200, {'a': 'b'});
      });
      it('Authenticates user', (done) => {
          let snowClient = require('../Client');
          snowClient = new snowClient(MY_URL, 'user', 'pass', 'application/json', 'application/json');      
    
          snowClient.getSingleRecord('test_table', 'test_sys_id', (response) => {
              console.log(response);
              expect(typeof response).to.be.a('string');
              // expect(JSON.parse(response).a).to.equal('b');
              done();
          });
      });
    });
    

    【讨论】:

    • 以前我只在 beforeEach 中使用 nock,但它仍然给出同样的错误。
    • 你能告诉我这个函数“snowClient.getSingleRecord”的定义吗
    猜你喜欢
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多