简体   繁体   English

Promise.then不在Jasmine中执行

[英]Promise.then is not executing in Jasmine

JavaScript - Jasmine test framework question JavaScript-Jasmine测试框架问题

Hi guys, for some reason, helpers.getWeatherData returns a promise but I can't manage to resolve it with the .then 嗨,大家好,由于某些原因,helpers.getWeatherData返回了一个承诺,但是我无法使用.then来解决它。

Can't find to much documentation on this either. 在这方面也找不到太多的文档。

Help would be amazing! 帮助将是惊人的!

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()

The problem may be that the beforeEach funciton requires done as a parameter when doing things asynchronously. 问题可能是在beforeEach操作时, beforeEach需要作为参数done

It would run the beforeEach function without waiting for the promise to fully execute. 它将运行beforeEach函数,而无需等待promise完全执行。

To fix, you could try this: 要解决此问题,您可以尝试以下操作:

  beforeEach(function(done) {
      console.log('hello');
      helpers.getWeatherData(URL).then((arr) => {
        console.log(arr.length, 'length');
        done();
      })
  })

See https://jasmine.github.io/2.4/introduction.html#section-Asynchronous_Support 参见https://jasmine.github.io/2.4/introduction.html#section-Asynchronous_Support

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM