无效的Chai属性:nock上的setEncoding

时间:2019-04-06 09:13:57

标签: node.js chai nock requestjs

我当时使用void merge(int A[], int p, int q, int r) { int *tmpL; int n1, n2; int i, j, k; // It is much simpler to consider q to point to the first element of // the second subarray and r to point after the last element of that array. q++; r++; n1 = q - p; // number of elements in the left sorted subarray n2 = r - q; // number of elements in the right sorted subarray tmpL = (int *)malloc(sizeof(int) * n1); if (tmpL == NULL) { // Report this fatal error or fall back to a different // algorithm that does not require allocation, such as // heapsort or insertion sort. return; } // Make a copy of the left subarray as elements may be overwritten in the loop. for (i = 0; i < n1; i++) { tmpL[i] = A[p + i]; } // Merge the elements of the subarrays: // - if all elements of the left array have been merged, // the remaining elements of the right subarray are already in place // - if k has reached r, all elements have been sorted for (i = j = 0, k = p; i < n1 && k < r; k++) { if (j >= n2 || tmpL[i] <= A[q + j]) { // Take the element from tmpL if the right subarray is empty // or if it is no greater than the next one from the right subarray. A[k] = tmpL[i]; i++; } else { // Otherwise take the element from the right subarray. A[k] = a[q + j]; j++; } } free(tmpL); } nock 9.1.6模拟nylas API。它与代码完美配合:

chai 3.5.0

我尝试将nock升级到10.0.6,将chai升级到4.2.0,现在出现以下错误:

it('fails when no state is given and provider fails', () => {
  const user = fixture.elements.user1;
  const connector = new NylasConnector('nylas', 'connect', user);

  nock(NYLAS_API)
        .post(`/a/${DN_NYLAS_APP_ID}/accounts/${user.nylas.account_id}/downgrade`)
        .reply(500, (uri, body) => expect(body).to.eql(''));

  sinon.stub(Nylas, 'urlForAuthentication').callsFake(params => {
    expect(params).to.eql({
      redirectURI: `${DN_AUTH_SERVICE_URL}/oauth2/nylas/callback`,
      trial: false,
      state: JSON.stringify({})
    });
    return chance.url() + '?' + objectToQuery(params);
  });

  return expect(connector.getAuthenticationUrl()).to.be.rejectedWith(ProviderError)
    .then(() => User.findById(user._id))
    .then(user => {
       expect(user.nylas).not.null;
       expect(user.nylasToken).not.null;
       expect(user.nylasCursor).not.null;
     });
  })

该错误出现在"Error: Invalid Chai property: setEncoding", " at Object.proxyGetter [as get] (/home/jeremy/datananas/datananas/packages/api/node_modules/chai/lib/chai/utils/proxify.js:78:17)", " at Object.isStream (/home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/common.js:382:24)", " at continueWithResponseBody (/home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/request_overrider.js:435:20)", " at end (/home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/request_overrider.js:385:12)", " at /home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/request_overrider.js:160:9", " at OverriddenClientRequest.RequestOverrider.req.write (/home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/request_overrider.js:139:9)", " at OverriddenClientRequest.RequestOverrider.req.end (/home/jeremy/datananas/datananas/packages/api/node_modules/nock/lib/request_overrider.js:156:11)", " at Request.end (/home/jeremy/datananas/datananas/packages/api/node_modules/request/request.js:1514:14)", " at end (/home/jeremy/datananas/datananas/packages/api/node_modules/request/request.js:564:14)", " at Immediate.<anonymous> (/home/jeremy/datananas/datananas/packages/api/node_modules/request/request.js:578:7)", " at runCallback (timers.js:705:18)", " at tryOnImmediate (timers.js:676:5)", " at processImmediate (timers.js:658:5)" 上,因为该函数调用了模拟函数。在此函数中,它们是对connector.getAuthenticationUrl()的调用,并且在到达带有nylasRequest的行时出现错误:

new Promise()

我使用function nylasRequest(uri, token, body) { const params = { uri: `https://api.nylas.com${uri}`, method: 'POST', json: body }; if (token) params.auth = { user: token }; console.log('This line appears. All params are valid.') return new Promise((resolve, reject) => request(params, (error, res, body) => { console.log('This line never shows up'); // some code })); } ,但没有更改所使用的版本。

1 个答案:

答案 0 :(得分:0)

我假设您的问题在于此行:

.reply(500, (uri, body) => expect(body).to.eql(''))

Nock将使用该函数返回的值作为伪响应的主体。当函数试图确定响应主体是否可流式传输时,从该函数返回chai期望似乎会使nock感到困惑。

我将其更改为以下内容:

nock(NYLAS_API)
  .post(`/a/${DN_NYLAS_APP_ID}/accounts/${user.nylas.account_id}/downgrade`)
  .reply(500, (uri, body) => {
    expect(body).to.eql('')
    return ''
  });