这是我的功能:
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不是一个函数(评估'(0,_MANAGEDRECORDS2.DEFAULT(((。然后,在test/api/api/ape-takaned-record-test.js中(第149行(中(check('( webpack:///test/api/managed-record-test.js:91:20&lt; - test/api/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);
});
});
这是来自软件包的依赖性和dev依赖性。JSON
"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浏览器运行,并且会遇到相同的错误。
因此,事实证明我的测试是在寻找对象的承诺,而我正在返回实际对象。