简体   繁体   English

如何使用Angular对带有promise和回调函数的函数进行单元测试

[英]How to unit test a function with a promise and callback function with Angular

Bit stumped on how to combine $httpBackend and promises with unit tests. 有点困惑如何将$ httpBackend和promise与单元测试结合起来。 The function I'm trying to test (in a controller) is: 我要测试的功能(在控制器中)是:

  vm.getData = (callback) => {
    $http.get('http://localhost:3000/cars?_start=0&_end=100').then(result => {
      return callback(result.data);
    });
  };

The test that's failing looks something like this... 失败的测试看起来像这样...

  describe('controllerToTest', () => {
    beforeEach(() => {
      angular.module('test', [])
      .controller('controllerToTest', require('./controllerToTest'));
    });

    beforeEach(angular.mock.module('test'));

    beforeEach(angular.mock.inject($injector => {
      $rootScope = $injector.get('$rootScope').$new(true);
      $q = $injector.get('$q').defer();
      $httpBackend = $injector.get('$httpBackend');
      $httpBackend.when("GET", "http://localhost:3000/cars?_start=0&_end=100").respond({});
      vm = $injector.get('$controller')('controllerToTest', {});
    }));


    it('should return the callback function', () => {
      let whatAmI = null;
      let callbackFn = () => {
        whatAmI = 'boom'
      };

      $q.resolve(
        (x) => {
          return x();
        }
      );

      vm.getData(callbackFn);

      $rootScope.$digest();

      expect(whatAmI).toEqual('boom');
    });
  }

$httpBackend doesn't return result synchronously. $httpBackend不同步返回结果。 To get response from pending $httpBackend requests $httpBackend.flush() method should be used: 要获得未决的$httpBackend请求的响应,应使用$httpBackend.flush()方法:

...
vm.getData(callbackFn);

$httpBackend.flush();

$rootScope.$digest();

expect(whatAmI).toEqual('boom');
...

Just make sure $httpBackend is available in it function's scope 只要确保$httpBackendit功能范围内可用

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

相关问题 Sinon 单元测试。 如何对将返回 promise 的 function 进行单元测试,该 function 将通过回调调用另一个 function? - Sinon unit testing. How to unit test a function that will return a promise that will call another function with callback? 如何模拟回调函数以在 Angular 8 中使用茉莉花测试承诺 - How to mock a callback function to test a promise in with jasmine in Angular 8 AngularJS:如何测试在回调函数中解决的promise - AngularJS : How to test a promise resolved in a callback function 如何使用Promise编写功能的单元测试 - How to write a unit test for a function with Promise 如何在非Promise函数中测试Promise回调? - How to test a Promise callback inside a non-Promise function? 功能返回承诺的单元测试 - Unit test for function returning Promise 如何使用回调 function 作为参数为 function 编写摩卡测试并返回 promise - How to write mocha test for a function with callback function as argument and returns a promise 单元测试请求-Promise,promise()不是函数 - Unit test request-promise, promise() is not a function 如何在AngularJS中对诺言/回调链进行单元测试? - How to unit test promise/callback chains in AngularJS? 如何对具有依赖性的函数进行单元测试(角度) - How to unit test a function with a dependency (angular)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM