Last active
April 23, 2018 08:02
-
-
Save revolunet/6722092 to your computer and use it in GitHub Desktop.
Jasmine test angular-translate calls
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
/* | |
silly way to ensure $translate service has been called with correct key and arguments | |
*/ | |
var $translate; | |
beforeEach(inject(function (_$translate_) { | |
$translate = function() { | |
$translate.spy.apply(this, arguments); | |
}; | |
$translate.spy = function() {}; | |
spyOn($translate, 'spy').andCallFake(function() { | |
// dummy | |
}); | |
}); | |
it('should call translate service with correct parameters', function() { | |
// check we call $translate('translation-key-to-check', {username: 'Bill Cosby'}); | |
expect($translate.spy).toHaveBeenCalledWith('translation-key-to-check', {username: 'Bill Cosby'}); | |
}); |
Thanks. I think there is a missing ')' on line 14.
not worked for me. Have used this code to mock $translate finally
beforeEach(module('MODULE_NAME, ($provide) => {
// emulation of making translate mock
const injector = angular.injector(['ng']),
$q = injector.get('$q');
const $translateMock = jasmine.createSpy().and.callFake(() => $q.when());
$translateMock.storageKey = () => '';
$translateMock.storage = () => { get: () => ''; };
$translateMock.preferredLanguage = function () {
return 'en';
};
$translateMock.use = () => '';
$provide.value('$translate', $translateMock);
}));
it('should have $translate called', inject(($translate) => {
$translate();
expect($translate).toHaveBeenCalled();
}));
Thanks @ralfius your code worked for me too :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, I was looking for a hour on how to do this. I was looking for something more "angularish" but this will work.