后来我如何在茉莉花中测试这个诺言的内在因素?

时间:2018-09-06 15:28:52

标签: javascript angularjs unit-testing promise jasmine

以下异步承诺位于我正在测试的Angular目标中。我看不到正在调用的“ then”中的代码,而这正是我需要测试的“ then”中的代码。

angular.module('Ls', [
])

function Locale($rootScope, $http) {

  var API = {
    getAvailables: getAvailables

  };
  API.getAvailables().then(function(data) {
        ..........do stuff
     ........it what this code is doing that I want to test!!!

      });

 function getAvailables() {
      return $http.get('/l.json').then(function(response) {
        return response.data;
    });
 }

2 个答案:

答案 0 :(得分:0)

您可以对getAvailables进行存根以始终进行解析,也可以将回调提取到单独的函数中并进行测试。

选项A

sinon.stub(API, 'getAvailables').resolves(data);

选项B

API.getAvailables().then(handleResponse);

^测试handleResponse函数

答案 1 :(得分:0)

这为我解决了 $ httpBackend.flush() https://docs.angularjs.org/api/ng/service/ $ http#writing-unit-tests-that-use-http

相关问题