nock scope.isDone():如何确定没有达到什么期望?

nock scope.isDone(): How to determine what expectation was not met?

我想使用 nock. My issue is with scoping function .isDone() 来模拟测试 Dropbox API,以确定是否满足预期。

添加到 nock.post() 的 console.log 显示匹配为真:

matching https://api.dropboxapi.com:443/2/files/create_folder_v2 to POST https://api.dropboxapi.com:443/2/files/create_folder_v2: true

但是 expect(scope.isDone()).to.be.true; 失败了。

我如何确定未达到的期望?

import { v4 } from 'uuid';
const request = require('request');
import nock = require('nock');
import { expect } from 'chai';

const accessToken = v4();
const folderName = v4();
const folderPath = `/${folderName}`;

let params = {
    'path': folderPath,
    'autorename': false
};

describe('Run Dropbox Archive Tests', function() {
    it('Create a Random Folder using Request', async function() {
        const scope = nock('https://api.dropboxapi.com/2', {
            reqheaders: {
                'authorization': `Bearer ${accessToken}`
            }
        })
            .log(console.log)
            .post('/files/create_folder_v2', params)
            .reply(200, (uri, requestBody) => {
                return {
                    metadata: {
                        name: folderName,
                        path_lower: folderPath,
                        path_display: folderPath,
                        id: `id:${v4()}`
                    }
                };
            });

        request.post({
            url: 'https://api.dropboxapi.com/2/files/create_folder_v2',
            headers: {
                authorization: `Bearer ${accessToken}`
            },
            json: params
        }, function(err, res) {
            if (err) {
                console.error(`err: ${JSON.stringify(err, null, 2)}`);
            } else {
                console.log(`res: ${JSON.stringify(res, null, 2)}`);
            }
        });

        expect(scope.isDone()).to.be.true;
    });
});

request.post的回复如下:

res: {
  "statusCode": 200,
  "body": {
    "metadata": {
      "name": "f303bd77-792a-44b5-844b-5ee29a5d4d44",
      "path_lower": "/f303bd77-792a-44b5-844b-5ee29a5d4d44",
      "path_display": "/f303bd77-792a-44b5-844b-5ee29a5d4d44",
      "id": "id:5760a2b4-73e2-4c85-8d2a-17450bd70e69"
    }
  },
  "headers": {
    "content-type": "application/json"
  },
  "request": {
    "uri": {
      "protocol": "https:",
      "slashes": true,
      "auth": null,
      "host": "api.dropboxapi.com",
      "port": 443,
      "hostname": "api.dropboxapi.com",
      "hash": null,
      "search": null,
      "query": null,
      "pathname": "/2/files/create_folder_v2",
      "path": "/2/files/create_folder_v2",
      "href": "https://api.dropboxapi.com/2/files/create_folder_v2"
    },
    "method": "POST",
    "headers": {
      "authorization": "Bearer b7f78d23-7033-4c7b-ba24-571ca97a2b42",
      "accept": "application/json",
      "content-type": "application/json",
      "content-length": 67
    }
  }
}

谢谢,非常感谢任何帮助。

在单个作用域或全局 Nock 实例上使用 .pendingMocks 来获取描述哪些 Interceptors 不是 "done".

的字符串列表
if (!scope.isDone()) {
  console.error('pending mocks: %j', scope.pendingMocks())
}

您似乎在使用 Mocha,这是我使用的一个片段。这是在全球测试运行器上的 afterEach

afterEach(function() {
  // https://github.com/mochajs/mocha/wiki/HOW-TO:-Conditionally-fail-a-test-after-completion
  if (this.test && !lodash.has(this.test, "ctx.currentTest.err")) {
    const pendingMocks = nock.pendingMocks();
    if (pendingMocks.length) {
      const msg = ["Not all nock mocks were used:"]
        .concat(pendingMocks)
        .join("\n\t");
      this.test.error(msg);
    }
  }

  nock.cleanAll();
});

如果测试尚未失败,并且仍有挂起的模拟,则测试将收到一个错误,列出所有挂起的拦截器。 cleanAll 然后被调用以删除任何持久的或可选的模拟。