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

查看:18
本文介绍了测试 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天全站免登陆