Axios请求抛出未知错误

时间:2017-10-06 23:37:32

标签: javascript node.js reactjs protocol-buffers jestjs

我正在尝试对我正在处理的应用进行单元测试,而且我遇到了一个使用axios发出AJAX请求的功能。这是我为它编写的模拟函数(注意:MockPBufData.txt文件是一个使用协议缓冲区序列化的文件,我从datamine.mta.info下载。可能下载该文件搞砸了它格式化并导致此错误?):

var GtfsRealtimeBindings = require('gtfs-realtime-bindings');
var axios = require('axios');

function RequestMock () {
    axios.get('./MockPBufData.txt')
        .then(function (body) {
            var feedData = body.data;
            var feed = GtfsRealtimeBindings.FeedMessage.decode(feedData);
            return { feed: feed };
        }).catch(error => console.log(error));
}

export default RequestMock;

这里的测试失败了(我期待的属性出现在你在datamine.mta.info上找到的每个反序列化文件中):

import React from 'react';
import RequestMock from '../mocks/RequestMock';

it('makes decoded data accessible from RequestMock.feed', () => {

expect(RequestMock.feed).toHaveProperty('header.gtfs_realtime_version', '1.0');
});

最后,这里是Jest错误以及来自控制台的堆栈跟踪:

expect(object)[.not].toHaveProperty(path, value)

    Expected object to be an object. Received:
      undefined: undefined

      at Object.<anonymous>.it (src/tests/api.test.js:12:37)
      at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)
      at process._tickCallback (internal/process/next_tick.js:109:7)


{ Error: Network Error
          at createError (/Users/Ben/React/subway-checker/node_modules/axios/lib/core/createError.js:16:15)
          at XMLHttpRequest.handleError [as onerror] (/Users/Ben/React/subway-checker/node_modules/axios/lib/adapters/xhr.js:87:14)
          at XMLHttpRequest.callback.(anonymous function) (/Users/Ben/React/subway-checker/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:289:32)
          at invokeEventListeners (/Users/Ben/React/subway-checker/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:219:27)
          at invokeInlineListeners (/Users/Ben/React/subway-checker/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:166:7)
          at EventTargetImpl._dispatch (/Users/Ben/React/subway-checker/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:122:7)
          at EventTargetImpl.dispatchEvent (/Users/Ben/React/subway-checker/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:87:17)
          at XMLHttpRequest.dispatchEvent (/Users/Ben/React/subway-checker/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:61:35)
          at XMLHttpRequest.abort (/Users/Ben/React/subway-checker/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:405:16)
          at Object.abort (/Users/Ben/React/subway-checker/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:315:13)
          at RequestManager.close (/Users/Ben/React/subway-checker/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:146:21)
          at Window.close (/Users/Ben/React/subway-checker/node_modules/jsdom/lib/jsdom/browser/Window.js:362:29)
          at JSDOMEnvironment.dispose (/Users/Ben/React/subway-checker/node_modules/jest-environment-jsdom/build/index.js:44:19)
          at Promise.resolve.then (/Users/Ben/React/subway-checker/node_modules/jest/node_modules/jest-cli/build/runTest.js:102:17)
          at process._tickCallback (internal/process/next_tick.js:109:7)
        config: 
         { adapter: [Function: xhrAdapter],
           transformRequest: { '0': [Function: transformRequest] },
           transformResponse: { '0': [Function: transformResponse] },
           timeout: 0,
           xsrfCookieName: 'XSRF-TOKEN',
           xsrfHeaderName: 'X-XSRF-TOKEN',
           maxContentLength: -1,
           validateStatus: [Function: validateStatus],
           headers: { Accept: 'application/json, text/plain, */*' },
           method: 'get',
           url: './MockPBufData.txt',
           data: undefined },
        request: 
         XMLHttpRequest {
           onabort: null,
           onerror: [Function: handleError],
           onload: null,
           onloadend: null,
           onloadstart: null,
           onprogress: null,
           ontimeout: [Function: handleTimeout],
           upload: 
            XMLHttpRequestUpload {
              onabort: null,
              onerror: null,
              onload: null,
              onloadend: null,
              onloadstart: null,
              onprogress: null,
              ontimeout: null,
              _ownerDocument: [Object] },
           onreadystatechange: [Function: handleLoad] },
        response: undefined }

1 个答案:

答案 0 :(得分:0)

您无法访问函数的内部变量(RequestMock.feed)...该函数实际返回它,因此您只需要以下内容:

expect(RequestMock()).toHaveProperty('header.gtfs_realtime_version', '1.0');