-
-
Save emiaj/7209915 to your computer and use it in GitHub Desktop.
// Global jasmine hook to prevent errors when attempt to | |
// download a localization .json file in test environment. | |
// Here we are resetting the translate provider to a | |
// safe state. | |
// If a test needs to assert for localized texts, | |
// the test author is free to setup the translate provider | |
// on isolation by overriding its settings. | |
// This wont affect other tests because once the test execution | |
// exits, the translate provider is reset once again to | |
// a safe state. | |
// The error we get if we do not add this global hook is: | |
// Error: Unexpected request: GET /locale.en.json | |
// No more request expected | |
// https://github.com/PascalPrecht/angular-translate/issues/42 | |
beforeEach(function() { | |
var DEFAULT_LANG = 'en'; | |
var DEFAULT_TRANSLATIONS = {}; | |
var MODULE_NAME = 'myapp.i18n'; // UPDATE ACCORDINGLY | |
module(MODULE_NAME, function config($translateProvider) { | |
$translateProvider.translations(DEFAULT_LANG, DEFAULT_TRANSLATIONS); | |
$translateProvider.preferredLanguage(DEFAULT_LANG); | |
}); | |
}); |
@kaczmar2, the error seems to be unrelated to the problem this code snippet solves...
Perhaps this could help: http://stackoverflow.com/questions/19801467/angular-multiple-directives-asking-for-templates-on
@emiaj Issue was when setting up directive unit tests - this had to be done in a particular way. Otherwise, we would get the "multiple directives" error. You need to move the directive setup code before the mock data and call an additional $digest after the mock data setup:
// Wrong way
beforeEach(inject(function($rootScope, $compile)
{
... mock data ...
confirmMessage = angular.element('<confirm-message/>');
$compile(confirmMessage)(scope);
scope.$digest();
}));
// Correct way: we need the following workaround
beforeEach(inject(function($rootScope, $compile)
{
confirmMessage = angular.element('<confirm-message/>');
$compile(confirmMessage)(scope);
scope.$digest();
... mock data ...
scope.$digest();
}));
@emiaj, how are you calling this "global hook" in Karma?
I'm running a Yeoman-scaffolded project that sources everything in tests/mock/**/*.js
before running the actual specs. I've tried adding your snippet above into a mock module and adding it as a dependency to each spec, to no avail.
When using this global hook, I get [$compile:multidir] Multiple directives error: ex: error: [$compile:multidir] Multiple directives [myDirectiveA, myDirectiveB] asking for template on <div id...
Is there a way to prevent this from happening?