测试ui-router stateprovider的'resolve:'值 [英] testing ui-router stateprovider 'resolve:' values

查看:104
本文介绍了测试ui-router stateprovider的'resolve:'值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jasmine + karma运行以下代码... 并得到以下错误:

I'm using jasmine+karma to run the following code... and get the following error:

Expected { then : Function, catch : Function, finally : Function } to equal 123.

有人可以帮助我理解为什么我的诺言没有得到解决的价值.谢谢

Can someone help me understand why I don't get a resolved value for my promise. thanks

'use strict';

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

  .config(function($stateProvider) {
    $stateProvider
      .state('stateOne', {
        url: '/stateOne',
        resolve: {cb: function($q) {
          var deferred = $q.defer();
          deferred.resolve(123);
          return deferred.promise;
        }},
        controller: function($scope, cb) {console.log(' * in controller', cb);},
        templateUrl: 'stateOne.html'
      });
  })

  .run(function($templateCache) {
    $templateCache.put('stateOne.html', 'This is the content of the template');
  });

describe('main tests', function() {
  beforeEach(function() {module('example');});

  describe('basic test', function($rootScope) {
    it('stateOne', inject(function($rootScope, $state, $injector, $compile) {

      var config = $state.get('stateOne');
      expect(config.url).toEqual('/stateOne');

      $compile('<div ui-view/>')($rootScope);
      $rootScope.$digest();
      expect($injector.invoke(config.resolve.cb)).toEqual(123);
    }));
  });

});

推荐答案

好吧,在Nikas的帮助下(通过电子邮件)弄清楚了这一点,我在其博客中找到了该网站: http://nikas.praninskas.com/angular/2014/09/27/unit-testing-ui-router-configuration/.

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/.

这是一个简洁的示例,演示了如何在ui.router中测试解析值,其中值涉及$ http.

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();
  }));

});

这篇关于测试ui-router stateprovider的'resolve:'值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆