testing ui-router stateprovider 'resolve:' values

不羁岁月 提交于 2019-12-03 22:22:56

Ok, Figured it out with some help (via email) from Nikas, whose blog I found at: http://nikas.praninskas.com/angular/2014/09/27/unit-testing-ui-router-configuration/.

Here is a succinct example that demonstrates how to test the resolve values in ui.router, where the values involve $http.

angular
  .module('example', ['ui.router'])

  .factory('Clipboard', function($http) {
    return {
      get: function(args) {
        return $http.get('/db/clipboard');
      }
    };
  })

  .config(function($stateProvider) {
    $stateProvider
      .state('stateOne', {
        resolve: {cb: function(Clipboard) {
          return Clipboard.get();
        }}
      });
  });

describe('main tests', function() {

  beforeEach(function() {module('example');});

  it('stateOne', inject(function($state, $injector, $httpBackend) {
    $httpBackend.whenGET('/db/clipboard').respond({a:1});

    $injector.invoke($state.get('stateOne').resolve['cb'])
      .then(function(res) {console.log(' *res ', res.data);})
      .catch(function(err) {console.log(' *err ', err);});
    $httpBackend.flush();
  }));

  afterEach(inject(function($httpBackend) {
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
  }));

});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!