0

这是我的功能:

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

4

1 回答 1

0

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

于 2018-04-06T14:58:40.627 回答