TypeError:undefined不是karma测试的函数,但是我从函数本身得到了预期的结果

时间:2018-04-03 02:34:45

标签: unit-testing karma-jasmine babel-polyfill

这是我的功能:

    const retrieve = (options = {page: 1, colors: []}) => {
        const limit = 10;
        var page = options.page || 1;
        const offset = (page * limit)-10;
        let color = options.colors || [];
        if (color.length === 1) {
             color[1] = "fjkdow";
        }
        if (page > 1) {
            output.previousPage = page -1;
        } else {
            output.previousPage = null;
        }
        if (page <50){
            output.nextPage = page + 1;
        } else {
            output.nextPage =  null;
        }
        //(limit=5, offset=0, color=['brown', 'green']);
        var template = URI(window.path).query({limit: limit, offset: offset, 
        color: color})
       fetch(template)
       .then(function(response) {
           if (response.status === 404) {
           console.log('404 not found')
        }
        return response.json()
        }).then(function(json) {
            isPrimary(json)
            getIds(json)
            getOpen(json)
            getClosed(json)
            if(json.length === 0 && page === 1) {
                output.nextPage =  null;
            }
        }).catch(function(ex) {
            console.log('parsing failed')
        })
        return output
    }

    export default retrieve;

该功能正常,我对控制台的输出与预期结果相符。我得到了一个测试文件来运行该函数。当我运行测试时,我收到错误:

TypeError:undefined不是一个函数(在test / api / managed-record-test.js中评估'(0,_managedRecords2.default)()。然后(check)')(第149行)     webpack:///test/api/managed-record-test.js:91:20&lt; - test / api / managed-record-test.js:149:42

测试如下:

    describe("Records", function() {

      it('should return unfiltered results', function(done){
        var expected = {"previousPage":null,"nextPage":2,
         "ids": [1,2,3,4,5,6,7,8,9,10],"open":  
         [{"id":2,"color":"yellow","disposition":"open","isPrimary":true},
          {"id":4,"color":"brown","disposition":"open","isPrimary":false},
          {"id":6,"color":"blue","disposition":"open","isPrimary":true},
          {"id":8,"color":"green","disposition":"open","isPrimary":false},
          {"id":10,"color":"red","disposition":"open","isPrimary":true}],
          "closedPrimaryCount":1};
        retrieve().then(function(output){
          expect(output).toEqual(expected);
        }).then(done);
      });
    });

以下是package.json

中的依赖项和devDependencies
    "dependencies": {
      "babel-core": "^6.21.0",
      "babel-loader": "6.4.0",
      "babel-polyfill": "^6.20.0",
      "babel-preset-env": "^1.4.0",
      "es6-promise": "^4.0.5",
      "express": "^4.15.2",
      "fetch-ponyfill": "^3.0.2",
      "karma-sourcemap-loader": "^0.3.7",
      "urijs": "^1.18.4"
    },
    "devDependencies": {
      "jasmine-core": "^2.4.1",
      "karma": "^1.7.0",
      "karma-chrome-launcher": "^0.2.2",
      "karma-jasmine": "^0.3.0",
      "karma-phantomjs-launcher": "^1.0.0",
      "karma-webpack": "^1.7.0",
      "phantomjs-prebuilt": "^2.1.7",
      "webpack": "^1.12.14"
    }

有人可以帮助我理解为什么测试会失败并且如何解决?我与PhantomJS和Chrome浏览器一起运行,我得到了同样的错误。

1 个答案:

答案 0 :(得分:0)

所以事实证明我的测试是寻找对象的Promise,而我正在返回实际的对象。