【问题标题】:TypeError: undefined is not a function with karma test but I get the expected result from the function itselfTypeError: undefined is not a function with karma test 但我从函数本身得到了预期的结果
【发布时间】:2018-04-03 02:34:45
【问题描述】:

这是我的功能:

    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 is not a function (evaluate '(0, _managedRecords2.default)().then(check)') in test/api/managed-record-test.js (line 149) webpack:///test/api/managed-record-test.js:91:20

测试看起来像这样:

    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 浏览器上都运行过,我得到了同样的错误。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-30
      • 2015-06-24
      • 2013-11-30
      • 2021-06-21
      相关资源
      最近更新 更多