このモジュールbxAuthモジュールを使用するテストがあり、次のように要求します。
beforeEach(module('bxAuth'));
このモジュールを機能させるには、構成する必要があります。したがって、モジュールが構成されていないときにエラーが発生することを予期するテストと、構成後にモジュール サービスが期待どおりに機能するかどうかを確認するテストがあります (各シナリオには独自の記述ブロックがあります)。
ただし、次のようにモジュールを構成すると (2 番目の記述ブロックのみ):
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();
});
});