尝试为https://github.com/beeman/loopback-angular-admin设置单元测试。
app / modules / about / controllers / about.controller.js(我添加了$scope.awesomeThings
来加载范围以及要测试的内容):
'use strict';
angular.module('com.module.about')
/**
* @ngdoc function
* @name com.module.about.controller:AboutCtrl
* @description
* # AboutCtrl
* Controller of the clientApp
*/
.controller('AboutCtrl', function($scope) {
$scope.angular = angular;
$scope.awesomeThings = [1, 2];
});
客户端/ test / modules / about / controllers / about.ctrl.js
上的jasmine测试'use strict';
describe('Controller: AboutCtrl', function () {
var AboutCtrl,
scope;
// load the controller's module
beforeEach(module('gettext'));
beforeEach(module('ui.router'));
beforeEach(module('com.module.about'));
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
AboutCtrl = $controller('AboutCtrl', {
'$scope': scope
});
}));
it('should attach a list of awesomeThings to the scope', function () {
expect(scope.awesomeThings.length).toBe(3);
});
});
当我运行这个简单的测试时,我得到:
TypeError: 'undefined' is not a function (evaluating '$rootScope.addDashboardBox(gettextCatalog.getString('About'), 'bg-maroon',
'ion-information', 0, 'app.about.index')')
at client/app/modules/about/controllers/about.config.js:6
at invoke (client/app/bower_components/angular/angular.js:4203)
at client/app/bower_components/angular/angular.js:4025
at forEach (client/app/bower_components/angular/angular.js:323)
at createInjector (client/app/bower_components/angular/angular.js:4025)
at workFn (client/app/bower_components/angular-mocks/angular-mocks.js:2425)
TypeError: 'undefined' is not an object (evaluating 'scope.awesomeThings')
at client/test/modules/about/controllers/about.ctrl.js:21
如果我设置logLevel:LOG_DEBUG,则about *文件显示:
- > %grep about /tmp/karma-debug.log
client/app/modules/about/app.about.js
client/app/modules/about/controllers/about.config.js
client/app/modules/about/controllers/about.controller.js
client/app/modules/about/controllers/about.routes.js
client/test/modules/about/controllers/about.ctrl.js
DEBUG [web-server]: serving (cached): client/app/modules/about/app.about.js
DEBUG [web-server]: serving (cached): client/app/modules/about/controllers/about.config.js
DEBUG [web-server]: serving (cached): client/app/modules/about/controllers/about.controller.js
DEBUG [web-server]: serving (cached): client/app/modules/about/controllers/about.routes.js
DEBUG [web-server]: serving (cached): client/test/modules/about/controllers/about.ctrl.js
我知道我遗漏了一些基本的东西,但我似乎无法找到什么。
答案 0 :(得分:7)
我没有仔细查看初始错误。实际错误在$rootScope.addDashboardBox
,表示需要包含其他模块。
解决方案是测试脚本:
'use strict';
describe('Controller: AboutCtrl', function () {
var AboutCtrl,
scope;
// load the controller's module
beforeEach(module('ui.router'));
beforeEach(module('gettext'));
beforeEach(module('formly'));
beforeEach(module('angular-loading-bar'));
beforeEach(module('lbServices'));
beforeEach(module('com.module.core'));
beforeEach(module('com.module.settings'));
beforeEach(module('com.module.about'));
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
AboutCtrl = $controller('AboutCtrl', {
'$scope': scope
});
}));
it('should attach a list of awesomeThings to the scope', function () {
expect(scope.awesomeThings.length).toBe(3);
});
});
答案 1 :(得分:7)
对于未来我来说,因为它是谷歌的第一个结果。
请查找外部依赖项!
Karma的日志有点误导,实际问题是主模块没有运行。例如,由Bower注入angular-stripe
的{{1}}需要加载实际的Stripe JS库,否则会崩溃整个应用程序(这本身就很烦人)。我已将此行添加到karma.conf.js
:
karma.conf.js
现在可行。