最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

nock scope.isDone():如何确定未满足什么期望?

运维笔记admin16浏览0评论

nock scope.isDone():如何确定未满足什么期望?

nock scope.isDone():如何确定未满足什么期望?

我希望使用nock模拟测试Dropbox API。我的问题是范围确定功能nock,该功能确定是否满足期望。

添加到.isDone()的console.log显示匹配为true:

.isDone()

但是nock.post()失败。

我如何确定未达到什么期望?

matching :443/2/files/create_folder_v2 to POST :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('', {
            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: '',
            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;
    });
});

谢谢,非常感谢您的协助。

回答如下:

在单独的作用域或全局Nock实例上使用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", "port": 443, "hostname": "api.dropboxapi", "hash": null, "search": null, "query": null, "pathname": "/2/files/create_folder_v2", "path": "/2/files/create_folder_v2", "href": "https://api.dropboxapi/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 } } } 未完成的字符串列表。

.pendingMocks

似乎您正在使用Interceptors,这是我使用的代码段。它位于全局测试运行器的if (!scope.isDone()) { console.error('pending mocks: %j', scope.pendingMocks()) } 中。

Mocha

如果测试尚未失败,并且仍然有待处理的模拟,那么该测试将收到列出所有待处理的拦截器的错误。然后调用afterEach以删除任何持久的或可选的模拟。

发布评论

评论列表(0)

  1. 暂无评论