Skip to content

Instantly share code, notes, and snippets.

@emiaj
Created October 29, 2013 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save emiaj/7209915 to your computer and use it in GitHub Desktop.
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
Copy link

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?

@emiaj
Copy link
Author

emiaj commented Jan 30, 2014

@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

@kaczmar2
Copy link

kaczmar2 commented Feb 3, 2014

@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();
}));

@tlvince
Copy link

tlvince commented Mar 3, 2014

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment