在 promise 解决后调用函数,但 Jasmine 未通过测试。为什么?

Function is called after promise is resolved but Jasmine fails the test. Why?

我的应用程序使用的服务 returns 通常依赖于一大堆其他承诺的承诺。我已将其重构为单独的命名函数,以使测试(和可读性)更容易。所以在这种情况下,我只想测试 运行 函数是否完成了它的工作并调用了其他函数。

例如

run() {
    return myService
    .connection
    .then(this.namedFunction1)
    .then(this.namedFunction2)
    .then(this.namedFunction3)
    .catch((error) => {
      console.log("doh!", error.stack);
    });

当我测试已调用 namedFunction1 时,Jasmine 失败了,尽管事实并非如此。这是我为了简单起见编写的一个小代码示例:

getString() {
    return Promise.resolve("Heeeelp. Heeeelp!!");
  }

  printToConsole(string) {
    console.log(string); // This works! but Jasmine says nay :( 
  }

  myFunction() {
    this.getString()
    .then(this.printToConsole)
    .catch((error) => {
      console.log("Some error occurred", error);
    });
  }

...和测试:

it("should call the printToConsole function", function() {
      spyOn(myClass, "printToConsole").and.callThrough(); //added the call through so it would print
      myClass.myFunction();
      expect(myClass.printToConsole).toHaveBeenCalled();
    });

和输出...

> Started F[2016-05-16 11:32:31.898] console - Heeeelp. Heeeelp!!
> 
> 
> Failures: 1) MyClass myFunction should call the printToConsole
> function   Message:
>     Expected spy printToConsole to have been called.   Stack:
>     Error: Expected spy printToConsole to have been called.

我试过添加 jasmine asynch done() 函数,但这什么也没做,最终我在示例中立即解决了这个承诺。

那么这个测试为什么或怎么会失败?

如有任何帮助,我们将不胜感激。谢谢

因为myFunction是一个异步操作。 myFunction 调用异步函数,然后立即调用 returns,之后测试断言触发。那时, printToConsole 还没有真正被调用。您需要使用 Jasmine's async test support 来 运行 此测试成功。

您需要将 myFunction 修改为 return 承诺,以便您知道它何时完成:

myFunction() {
  return this.getString()
  .then(this.printToConsole)
  .catch((error) => {
    console.log("Some error occurred", error);
  });
}

然后您将修改测试以使用 Jasmine 提供的 done 函数:

it("should call the printToConsole function", function(done) {
  spyOn(myClass, "printToConsole").and.callThrough(); //added the call through so it would print
  myClass.myFunction().then(function () {
    expect(myClass.printToConsole).toHaveBeenCalled();
    done();
  }).catch(done); // to make sure the test reports any errors
});

这应该能让事情正常进行。