angularjs – 错误:超时 – 在指定的超时期间未调用异步回调… .DEFAULT_TIMEOUT_INTERVAL

前端之家收集整理的这篇文章主要介绍了angularjs – 错误:超时 – 在指定的超时期间未调用异步回调… .DEFAULT_TIMEOUT_INTERVAL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个角度服务班:
  1. angular.module('triggerTips')
  2. .service('userData',function ($rootScope,$http,$log,$firebase) {
  3.  
  4. this._log = {
  5. service : 'userData'
  6. };
  7.  
  8. // Synchronized objects storing the user data
  9. var config;
  10. var userState;
  11.  
  12. // Loads the user data from firebase
  13. this.init = function(readyCallback) {
  14. var log = angular.extend({},this._log);
  15. log.funct = 'init';
  16.  
  17. var fireRef = new Firebase('https://XYZfirebaseio.com/' + $rootScope.clientName);
  18. config = $firebase(fireRef.child('config')).$asObject();
  19. userState = $firebase(fireRef.child('userState').child($rootScope.userName)).$asObject();
  20.  
  21. Promise.all([config.$loaded(),userState.$loaded()]).
  22. then(
  23. function() {
  24. if(config == null || Object.keys(config).length < 4) {
  25. log.message = 'Invalid config';
  26. $log.error(log);
  27. return;
  28. }
  29.  
  30. if(!userState.userProperties) {
  31. userState.userProperties = {};
  32. }
  33.  
  34. if(!userState.contentProperties) {
  35. userState.contentProperties = {};
  36. }
  37.  
  38. log.message = 'User Properties: ' + JSON.stringify(userState.userProperties);
  39. $log.debug(log);
  40.  
  41. log.message = 'Content Properties: ' + JSON.stringify(userState.contentProperties);
  42. $log.debug(log);
  43.  
  44. log.message = 'Loaded user data from firebase';
  45. $log.debug(log);
  46. readyCallback();
  47. },function() {
  48. log.message = 'Unable to load user data from firebase';
  49. $log.error(log);
  50. }
  51. );
  52. };
  53.  
  54. // Returns the initial tip configuration
  55. this.getConfig = function() {
  56. return config;
  57. };
  58.  
  59. // Set the value of a user property
  60. // A user property is something about the user himself
  61. this.setUserProperty = function(property,value) {
  62. if(!userState.userProperties) {
  63. userState.userProperties = {};
  64. }
  65. userState.userProperties[property] = value;
  66. userState.$save();
  67. $rootScope.$broadcast('user-property-change',property);
  68. };
  69.  
  70. // Get the value of a user property
  71. this.getUserProperty = function(property) {
  72. if(userState.userProperties) {
  73. return userState.userProperties[property];
  74. }
  75. };
  76.  
  77. // Set the value of a user content property
  78. // A content property is something about a particular peice of content for a particular user
  79. this.setContentProperty = function(contentName,property,value) {
  80. if(!userState.contentProperties[contentName]) {
  81. userState.contentProperties[contentName] = {};
  82. }
  83.  
  84. userState.contentProperties[contentName][property] = value;
  85. userState.$save();
  86. $rootScope.$broadcast('content-property-change',contentName,property);
  87. };
  88.  
  89. // Increment a count property on the user state for a given tip
  90. this.incrementContentProperty = function(contentName,property) {
  91. if(!userState.contentProperties[contentName]) {
  92. userState.contentProperties[contentName] = {};
  93. }
  94. if(!userState.contentProperties[contentName][property]) {
  95. userState.contentProperties[contentName][property] = 0;
  96. }
  97.  
  98. userState.contentProperties[contentName][property]++;
  99. userState.$save();
  100. $rootScope.$broadcast('content-property-change',property);
  101. };
  102.  
  103. // Returns the user state for a given tip and property
  104. this.getContentProperty = function(contentName,property) {
  105. if(userState.contentProperties) {
  106. var t = userState.contentProperties[contentName];
  107. if(t) {
  108. return t[property];
  109. }
  110. }
  111. };
  112. });

我试图用茉莉花单元测试这个服务:

我的单位测试是:

  1. 'use strict';
  2.  
  3. describe('Service: userData',function () {
  4.  
  5. // load the service's module
  6. beforeEach(function() {
  7. module('triggerTips');
  8. });
  9.  
  10. // instantiate service
  11. var userData;
  12. beforeEach(inject(function (_userData_) {
  13. userData = _userData_;
  14. }));
  15.  
  16. it('should load correctly',function () {
  17. expect(!!userData).toBe(true);
  18. });
  19.  
  20. describe('after being initialized',function () {
  21.  
  22. beforeEach(function(done) {
  23. // Unable to get this working because the callback is never called
  24. userData.init(function() {
  25. done();
  26. });
  27. jasmine.DEFAULT_TIMEOUT_INTERVAL = 2000;
  28. });
  29.  
  30. it('should have a valid config',function (done) {
  31. setTimeout(function() {
  32. expect(Object.keys(userData.getConfig()).length == 0);
  33. done();
  34. },1500);}); }); });

我阅读了关于Jasmine的异步支持,但是由于我相当新的使用JavaScript的单元测试无法使其工作.

我收到一个错误

Async callback was not invoked within timeout specified by
jasmine.DEFAULT_TIMEOUT_INTERVAL

有人可以帮我提供我的代码的工作示例一些解释吗?

我建议您用$timeout替换setTimeout,以加快您的规格套件.您将需要 ngMock作为您的规格套件的一部分,以使其以预期的方式工作,但这似乎已被照顾您的规格.好东西.

那么为了使规范的异步性质“走开”,你会呼吁:

$timeout.flush([delay])其中延迟是可选的.

>如果没有延迟通过,所有待处理的异步任务(有角度的世界)将完成他们在做什么.
>如果传递延迟,则指定延迟内的所有待处理任务将完成.指定延迟之外的人将保持在“待定”状态.

这样,您可以删除完成的回调并编写您的测试:

  1. describe('after being initialized',function () {
  2. var $timeout;
  3.  
  4. beforeEach(function () {
  5. // Unable to get this working because the callback is never called
  6. userData.init();
  7.  
  8. inject(function ($injector) {
  9. $timeout = $injector.get('$timeout');
  10. });
  11. }));
  12.  
  13. it('should have a valid config',function () {
  14. $timeout.flush();
  15. // callback should've been called now that we flushed().
  16. expect(Object.keys(userData.getConfig()).length).toEqual(0);
  17. });
  18. });

你使用什么承诺?我看到Promise.all的电话,但是为了继续我的答案,我将假设它相当于$q.all.运行$timeout.flush应该处理这些值.

如果你想对茉莉花的承诺的拒绝/解决的价值写出期望,我会研究一下像jasmine-promise-matchers这样的东西,使它干净漂亮,但是禁止你这样做:

  1. // $q
  2. function get () {
  3. var p1 = $timeout(function () { return 'x'; },250);
  4. var p2 = $timeout(function () { return 'y'; },2500);
  5.  
  6. return $q.all([p1,p2]);
  7. }
  8.  
  9. // expectation
  10. it('is correct',function () {
  11. var res;
  12.  
  13. get().then(function (r) {
  14. res = r;
  15. });
  16.  
  17. $timeout.flush(2500);
  18.  
  19. expect(res).toEqual(['x','y']);
  20. });

根据您的设置,您可能需要或可能不需要根据您的本地配置变量存储/监视(取决于您的框架定义的间谍)承诺,但这是我估计的另一个故事.

我不熟悉$firebase(某事)$asObject.$loaded – 因此我可能错过了这里的东西,但假设它的工作原理就像任何其他承诺一样,你应该很好.

jsfiddle

猜你在找的Angularjs相关文章