Testing Service Promise with Jasmine

21 views
Skip to first unread message

César Barone

unread,
Jul 3, 2015, 3:36:40 PM7/3/15
to ang...@googlegroups.com
Hi,

I am trying to test a method that returns a promise.  But i think promise is resolved after test finish. 


Can anyone help me?


César Barone

unread,
Jul 6, 2015, 5:24:23 PM7/6/15
to ang...@googlegroups.com
nobody?

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/uyvDBPJxPM0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Sander Elias

unread,
Jul 7, 2015, 1:31:47 AM7/7/15
to ang...@googlegroups.com
Hi César,

You need to call $digest before your promise will be resolved. Angular promises are async, so if you don't do a digest in between, your .then in the test will not be ran before your expect.

try:
describe('Testing promise', function() {
 
var $scope = null;
 
var myService = null;
 
var cachedInstance = {
    name
: 'John'
 
}
 
var rootScope = {}
 
//you need to indicate your module in a test
  beforeEach
(module('plunker'));


  beforeEach
(inject(function(_myService_,$rootScope) {
    myService
= _myService_;
    myService
.cachedInstance = cachedInstance;
    $scope
= $rootScope
 
}));


  it
('should return cached instance', function() {
   
var resolvedInstance = {}
    myService
.get().then(function(instance){
      resolvedInstance
= instance;
   
});
    $scope
.$digest()
    expect
(resolvedInstance).toBe(cachedInstance);
 
});
});



Regards
Sander
Reply all
Reply to author
Forward
0 new messages