Last active
January 20, 2016 16:40
-
-
Save dam1/a784c0a7dd0178fd5e6c to your computer and use it in GitHub Desktop.
Very simple mock function for the $translate service ( Angular js ) for Unit test with Jasmine / Karma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$translate Mock : | |
$translate=function (translation) { | |
return { | |
then: function (callback) { | |
var translated={}; | |
translation.map(function (transl) { | |
translated[transl]=transl; | |
}); | |
return callback(translated); | |
} | |
} | |
}; | |
Usage : | |
Test: | |
controller=$controller('LoginCtrl', { | |
'$scope': scope, | |
'$state': stateMock, | |
'$ionicPopup': ionicPopupMock, | |
'$translate': $translate, | |
'$ionicLoading': $ionicLoading, | |
} | |
); | |
... | |
it('should not login with wrong credentials ', function () { | |
rscope.AAuser.email='[email protected]'; | |
spyOn(scope, "showInvalidCredential").and.callThrough(); | |
scope.login(); | |
expect(scope.showInvalidCredential).toHaveBeenCalled(); | |
expect(ionicPopupMock.alert).toHaveBeenCalledWith({ | |
title: 'INVALID_CREDENTIALS', | |
template: 'TRY_AGAIN' | |
}); | |
}); | |
Angular Controller : | |
$scope.showInvalidCredential=function () { | |
$translate(['INVALID_CREDENTIALS', 'TRY_AGAIN']).then(function (translations) { | |
$ionicPopup.alert({ | |
title: translations.INVALID_CREDENTIALS, | |
template: translations.TRY_AGAIN | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment