Как настроить модуль в тестах кармы с помощью Jasmine
У меня есть тест, который использует этот модуль модуля bxAuth, и мне нужно это так:
beforeEach(module('bxAuth'));
Этот модуль должен быть настроен для работы. Поэтому у меня есть тест, который ожидает ошибки, когда модуль не настроен, и тесты, которые проверяют, работают ли сервисы модуля должным образом после конфигурации (каждый сценарий имеет свой собственный блок описаний).
Однако, если я настрою модуль следующим образом (только во втором блоке описания):
angular.module('bxAuth').config(ConfigureAuth);
мой модуль настроен во всех описывающих блоках, поэтому в основном он всегда настроен.
Вот полный код:
describe('not configured bxAuth module', function() {
beforeEach(module('bxAuth'));
var bxAuth;
beforeEach(inject(function (_bxAuth_) {
bxAuth = _bxAuth_;
}));
it('should throw exception without cofiguration', function () {
expect(bxAuth.authenticate).toThrow(); // **this expectation fails**
});
});
describe('configured bxAuth module', function () {
beforeEach(module('bxAuth'));
var bxAuth;
beforeEach(inject(function (_bxAuth_) {
bxAuth = _bxAuth_;
}));
angular.module('bxAuth').config(ConfigureAuth);
function ConfigureAuth(bxAuthProvider) {
bxAuthProvider.config.authenticate = function (username, password) {
var deferred = angular.injector(['ng']).get('$q').defer();
/* replace with real authentication call */
deferred.resolve({
id: 1234,
name: 'John Doe',
roles: ['ROLE1', 'ROLE2']
});
return deferred.promise;
}
}
it('should not throw exception with cofiguration', function () {
expect(bxAuth.authenticate).not.toThrow();
});
});