Jasmine 如何监视一个内部有承诺的函数,并且不';t返回结果,但自行处理响应?

Jasmine 如何监视一个内部有承诺的函数,并且不';t返回结果,但自行处理响应?,jasmine,es6-modules,Jasmine,Es6 Modules,如何在对象中监视名为placed的方法 // Game.js export default { mine: null, handle: function(me) { console.log(" FOOOOO " + me) }, setSource: function() { this.mine.getSource().then((response) => { const {source} = r

如何在对象中监视名为placed的方法

// Game.js

export default {
    mine: null,

    handle: function(me) {
        console.log("   FOOOOO " + me)
    },

    setSource: function() {
        this.mine.getSource().then((response) => {
          const {source} = response
          this.handle(source)
        })
    }
}
在这里,我试图窥探:

// GameSpec.js

import Game from '../../lib/jasmine_examples/Game'

Game.mine = {}

describe("Game", function() {
  it("should set source and handle it", function() {

    Game.mine.getSource = () => {
      return new Promise((resolve)=>{
        resolve( {
          source : 'BAAAAR'
        })
      })
    }

    spyOn(Game, 'handle').and.callThrough()

    Game.setSource()

    expect(Game.handle).toHaveBeenCalled()
  });
});
在输出中,您可以看到调用了函数“handle”:

Started
F   FOOOOO BAAAAR


Failures:
1) Game should set source and handle it
  Message:
    Expected spy handle to have been called.
  Stack:
    Error: Expected spy handle to have been called.
        at <Jasmine>
        at UserContext.<anonymous> (/Users/silverbook/Sites/zTest/jasmine/spec/jasmine_examples/PlayerSpec.js:20:29)
        at <Jasmine>

1 spec, 1 failure
已启动
福ooo巴阿
失败:
1) 游戏应该设置源代码并处理它
信息:
预期已调用间谍句柄。
堆栈:
错误:预期已调用间谍句柄。
在
在UserContext。(/Users/silverbook/Sites/zTest/jasmine/spec/jasmine_examples/PlayerSpec.js:20:29)
在
1个规格,1个故障
但是茉莉花说它没有被叫来

如果我删除模拟的
Promise
测试通过,但我需要它。在另一个测试中,我将在Promise中返回一个错误,并让它从另一个函数处理


因此,
Promise
会破坏测试,但为什么?

测试会同步执行,而
expect
会在
this.mine.getSource().then()排队的回调有机会执行之前失败

对于
Jasmine
>=2.7和
async
函数支持,您可以将测试函数转换为
async
函数,并添加
wait Promise.resolve()要暂停同步测试并让任何排队回调执行的位置

对于您的测试,它将如下所示:

import Game from '../../lib/jasmine_examples/Game'

Game.mine = {}

describe("Game", function() {
  it("should set source and handle it", async () => {

    Game.mine.getSource = () => {
      return new Promise((resolve)=>{
        resolve( {
          source : 'BAAAAR'
        })
      })
    }
    spyOn(Game, 'handle').and.callThrough();

    Game.setSource();

    await Promise.resolve(); // let the event loop cycle

    expect(Game.handle).toHaveBeenCalled();
  });
});
import Game from '../../lib/jasmine_examples/Game'

Game.mine = {}

describe("Game", function() {
  it("should set source and handle it", (done) => {

    Game.mine.getSource = () => {
      return new Promise((resolve)=>{
        resolve( {
          source : 'BAAAAR'
        })
      })
    }
    spyOn(Game, 'handle').and.callThrough();

    Game.setSource();

    Promise.resolve().then(() => {
      expect(Game.handle).toHaveBeenCalled();
      done();
    });

  });
});
对于较早的(>=2.0)
Jasmine
版本,您可以像这样使用
done()

import Game from '../../lib/jasmine_examples/Game'

Game.mine = {}

describe("Game", function() {
  it("should set source and handle it", async () => {

    Game.mine.getSource = () => {
      return new Promise((resolve)=>{
        resolve( {
          source : 'BAAAAR'
        })
      })
    }
    spyOn(Game, 'handle').and.callThrough();

    Game.setSource();

    await Promise.resolve(); // let the event loop cycle

    expect(Game.handle).toHaveBeenCalled();
  });
});
import Game from '../../lib/jasmine_examples/Game'

Game.mine = {}

describe("Game", function() {
  it("should set source and handle it", (done) => {

    Game.mine.getSource = () => {
      return new Promise((resolve)=>{
        resolve( {
          source : 'BAAAAR'
        })
      })
    }
    spyOn(Game, 'handle').and.callThrough();

    Game.setSource();

    Promise.resolve().then(() => {
      expect(Game.handle).toHaveBeenCalled();
      done();
    });

  });
});

您可以在
fakeAsync
中运行测试,并在
expect()之前运行
tick()

服务:

单元测试:


谢谢你的帮助!它产生了这个错误
ReferenceError:regeneratorRuntime没有定义
。@PaulGeisler这个错误意味着你没有设置
async
polyfill,我已经为较早版本的
Jasmine
和/或no
async
支持添加了一个示例,请告诉我它是否适用于你是的,你说得对-我们使用Jasmine 2.x。您的解决方案奏效了,谢谢@保罗·盖斯勒太棒了!