简体   繁体   English

使用Jasmine从API进行单元测试承诺

[英]Unit testing promises from an api using Jasmine

I wrote an API that is promised based. 我写了一个基于承诺的API。 Which is the best way to unit test it? 哪种是最好的单元测试方法? Is Jasmine a good approach? 茉莉花是好方法吗?

Also can on give me an example on how one can unit test promises? 也可以给我一个关于单元测试如何承诺的例子吗? Should one unit test also the "then" and also the "catch" part? 一个单元应该同时测试“ then”和“ catch”部分吗?

Jasmine is definitely a strong choice for doing promise based unit testing. Jasmine绝对是进行基于承诺的单元测试的理想选择。 Without knowing too much about your API, below are a few examples of how you can do unit testing while waiting for promise resolution or failure. 在不了解您的API的情况下,以下是一些示例,这些示例说明了如何在等待承诺解决方案或失败的同时进行单元测试。

The keyword in the sample below is the done variable passed in to each of the unit tests ( it blocks). 下面的示例中的关键字是传递给每个单元测试( it阻止)的done变量。 This lets jasmine know that the test is asynchronous and jasmine will wait for the done function to be called before moving on from that unit test. 这使jasmine知道测试是异步的,并且jasmine将在从该单元测试继续进行之前等待done函数被调用。

Hope this helps! 希望这可以帮助!

describe('Unit tests', function() {
  it('promise should succeed', function (done) {
    myApi.function().then(function(data) {
      //assert data came back correctly
      done();
    }).catch(function() {
      fail();
    });
  });

  it('promise should throw error', function() {
    myApi.function().then(function(data) {
      fail();
    }).catch(function(error) {
      //assert error has been thrown correctly
      done();
    });
  });
});

Jasmine is a unittest framework, it provides a test runner, assertion methods and mocks/spies based in. It is a popular way to unittest javascript code. Jasmine是一个单元测试框架,它提供了一个测试运行器,断言方法和基于它的模拟/间谍程序。这是对JavaScript代码进行单元测试的一种流行方法。 It is no better than any of the other ways / frameworks available and should be evaluated against the other options to see if it is a correct fit for your project. 它并不比任何其他可用的方法/框架都好,并且应该对照其他选项进行评估,以查看它是否适合您的项目。

Without example code, unittesting promises should be no different than unittesting any other code. 没有示例代码,单元测试的承诺应该与单元测试任何其他代码没有什么不同。 Stub the IO dependencies and evaluate all critical logic paths. 存根IO依赖关系并评估所有关键逻辑路径。 If your promise explicitly throws than catch could be a valuable method to assert on. 如果您的诺言明确抛出而不是catch可能是一个有价值的主张。 Same with then , calling then should allow you to test your resolve logic. then相同, then调用then应该允许您测试解析逻辑。

Having to interact with your code through then and catch may not be the most effective way to exercise your promise logic. 不必通过与您的代码进行交互thencatch可能无法行使自己的诺言逻辑的最有效途径。 If there is significant amount of logic it is good to encapsulate that in a function outside of promise, to allow for easier unittesting. 如果存在大量逻辑,则最好将其封装在promise之外的函数中,以简化单元测试。 Then the interface to that core logic could lightly be tested through resolving the promise. 然后,可以通过解决承诺来轻松测试与该核心逻辑的接口。

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

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