angularjs - Testing Angular Service in Karma: Same Test, Different Results -


i'm trying test angular service karma, , getting strange results, kicks made 2 tests identical see if same results, didn't:

describe('profile data service', function() {   var logindata, rootscope, $httpbackend;   beforeeach(module('myapp'));    describe('get profile data', function() {     beforeeach(inject(function(_logindata_, $rootscope, _$httpbackend_) {       logindata = _logindata_;       logindata.username= "jsmith";       logindata.token = "token";        rootscope = $rootscope;        $httpbackend = _$httpbackend_;      }));      it('should profile data on instantiation', inject(function(profiledataservice) {       $httpbackend.expect('post', 'http://mytestserver.com/api/', {         params: [{           username: "jsmith",           token: "token",           method: "getprofile"         }]       })       .respond({         result: {           address: "111 main st."           city: "los angeles",           state: "ca"         }       });       $httpbackend.flush();       expect(profiledataservice.profiledata.address).tomatch("111 main st.");     }));      it('should profile data on instantiation', inject(function(profiledataservice) {       $httpbackend.expect('post', 'http://mytestserver.com/api/', {         params: [{           username: "jsmith",           token: "token",           method: "getprofile"         }]       })       .respond({         result: {           address: "111 main st."           city: "los angeles",           state: "ca"         }       });       $httpbackend.flush();       expect(profiledataservice.profiledata.address).tomatch("111 main st.");     }));   }); }); 

the first test passes, second test states profiledata undefined. identical tests. assuming each it profiledataservice being re-initialized, may not case. if that's not true, how create separate tests service destroyed , re-initialized each test case?

the logic service straightforward:

(function() {   'use strict';   angular.module('servicesmodule')   .service('profiledataservice', profiledataservice);    profiledataservice.$inject = ["$http", "$q", "logindata", "cachefactory"];    function profiledataservice($http, $q, logindata, cachefactory) {     var profiledataservice = this;     (function() {       init();     })();      function init() {       getprofile().then(function(profiledata) {         proiledataservice.profiledata = profiledata;       });     }      function getprofile() {       var deferred = $q.defer();       var data = {         params: [{           username: logindata.username,           token: logindata.token,           method: "getprofile"         }]       };       var profiledatacache = cachefactory.get('profiledatacache');        if (profiledatacache.get('profiledata') && !invalidatecache) {         deferred.resolve(profiledatacache.get('profiledata'));       }       else {         $http.post('http://mytestserver.com/api/', data)         .success(function(data) {           profiledatacache.put('profiledata', response.result);           deferred.resolve(data.result);         });       }       return deferred.promise;     }   } })(); 

i should note i've tried adding rootscope.$digest() in different places in test, doesn't seem make difference. thought manually triggering digest cycle ensure http post caught , response mocked.

edit: left out huge detail... angular-cache. forgot mention using cacheing plugin. reason problem. after creating plunker , seeing getting consistent results identical tests, knew there failing realize. second test not making post request because of cacheing logic.

my issue failing realize use of cache giving me different results each test. specifically, service logic prevented second post api if cache had been persisted response.


Comments