Angularjs 如何使用Jasmine在Karma测试中配置模块
我有一个测试使用这个模块bxAuth模块,我要求它如下:Angularjs 如何使用Jasmine在Karma测试中配置模块,angularjs,jasmine,karma-runner,angular-mock,Angularjs,Jasmine,Karma Runner,Angular Mock,我有一个测试使用这个模块bxAuth模块,我要求它如下: beforeEach(module('bxAuth')); 必须配置此模块才能工作。所以我有一个测试,当模块未配置时会出现错误,并且测试会检查模块服务在配置后是否按预期工作每个场景都有自己的描述块 但是,如果我仅在第二个描述块中这样配置模块: angular.module('bxAuth').config(ConfigureAuth); 我的模块在所有descripe块中都进行了配置,因此基本上总是进行配置 以下是完整的代码: des
beforeEach(module('bxAuth'));
必须配置此模块才能工作。所以我有一个测试,当模块未配置时会出现错误,并且测试会检查模块服务在配置后是否按预期工作每个场景都有自己的描述块
但是,如果我仅在第二个描述块中这样配置模块:
angular.module('bxAuth').config(ConfigureAuth);
我的模块在所有descripe块中都进行了配置,因此基本上总是进行配置
以下是完整的代码:
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();
});
});