I'm testing a promise with angularjs jasmine, and sinonjs. I'm puzzled by something regarding promises. Here is my code:
it('should return data with length 4 ', inject(function ($rootScope) {
var storageData;
mockDualStorage.getData.returns($.when(''));
// mockDualStorage.getData is called by getStorageData
// $rootScope.$digest() // not working here
dataGetter.getStorageData().then(function (data) {
console.log(1);
storageData = data;
});
$rootScope.$digest(); // only working here
console.log(2);
expect(storageData.length).toBe(4)// ok
}));
Couple of things are strange here.
If I put $rootScope.$digest()
above the dataGetter.getStorageData()
then
function is never executed.
When the
$rootScope.$digest()
is below,then
gets executed, and order ofconsole.log
is 1,2Why won't
then
execute when$rootScope.$digest()
is above? As I understand promise is already resolved?