Skip to content

Instantly share code, notes, and snippets.

@dam1
Last active January 20, 2016 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dam1/a784c0a7dd0178fd5e6c to your computer and use it in GitHub Desktop.
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
$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='damien@gymbirds.com';
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