所以,这真的很愚蠢。但是 google 是不可能的,因为未定义的模块字面意思是 module
。我摆脱了单元测试中的所有其他 TS 错误,除了最后一个:
main/components/login/loginController.Spec.ts(12,7): error TS2304: Cannot find name 'module'.
有人说,“只包含 angular-mocks.js”,但我已经这样做了,它消除了一些其他错误,但不是这个错误。
这是我的引用资料:
/// <reference path="../../typings/angularjs/angular.d.ts" />
/// <reference path="../../typings/jquery/jquery.d.ts" />
/// <reference path="../../typings/d3/d3.d.ts" />
/// <reference path="../../typings/jasmine/jasmine.d.ts" />
/// <reference path="../../typings/angularjs/angular-mocks.d.ts" />
这里有一个简单的测试来说明这个问题。
describe('Login Controller Spec', function() {
beforeEach(function() {
// uh oh
module('App');
inject(function(_$controller_, $rootScope) {
scope = $rootScope.$new();
controller = _$controller_;
navCtrl = controller('loginController', { $scope: scope});
});
});
it('should call init', function(){
//...
});
});
我应该获取任何其他库的定义吗?
最佳答案
你可以一起去
angular.mock.module('App');
自 source code是
window.module = angular.mock.module = function() {
var moduleFns = Array.prototype.slice.call(arguments, 0);
return isSpecRunning() ? workFn() : workFn;
/////////////////////
function workFn() {
if (currentSpec.$injector) {
throw new Error('Injector already created, can not register a module!');
} else {
var fn, modules = currentSpec.$modules || (currentSpec.$modules = []);
angular.forEach(moduleFns, function(module) {
if (angular.isObject(module) && !angular.isArray(module)) {
fn = function($provide) {
angular.forEach(module, function(value, key) {
$provide.value(key, value);
});
};
} else {
fn = module;
}
if (currentSpec.$providerInjector) {
currentSpec.$providerInjector.invoke(fn);
} else {
modules.push(fn);
}
});
}
}
};
另一种可能是
window['module']('App'); // and maybe also window.module('App');
关于javascript - 如何在 Jasmine 单元测试中的 TypeScript 中获取 'Module' 的定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33838547/