javaScript-茉莉测试框架问题
大家好,由于某种原因,helpers.getweatherdata返回了一个诺言,但我无法用.then
来解决它也找不到太多文档。
帮助会很棒!
import Jasmine from 'jasmine'
const jasmine = new Jasmine()
jasmine.loadConfigFile('spec/support/jasmine.json')
import helpers from '../src/lib/helpers'
import fetch from 'node-fetch'
import Promise from 'bluebird'
describe("Weather Service App", function() {
const URL = `http://api.openweathermap.org/data/2.5/forecast?`;
beforeEach(function(){
console.log('hello')
helpers.getWeatherData(URL).then((arr)=>{
console.log(arr.length, 'length')
})
})
console.log(helpers.getWeatherData(URL))
it("Test API endpoint, return array of five objects", function() {
});
});
jasmine.execute()
问题可能是 beforeEach
funciton在异步进行操作时需要done
作为参数。
它将在不等待完全执行的承诺的情况下运行之前的功能。
要修复,您可以尝试以下操作:
beforeEach(function(done) {
console.log('hello');
helpers.getWeatherData(URL).then((arr) => {
console.log(arr.length, 'length');
done();
})
})
参见https://jasmine.github.io/2.4/introduction.html#section-section-asynchronous_support