Mik*_*owe 8 angularjs karma-jasmine
尝试为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];
});
Run Code Online (Sandbox Code Playgroud)
客户端/ 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);
});
});
Run Code Online (Sandbox Code Playgroud)
当我运行这个简单的测试时,我得到:
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
Run Code Online (Sandbox Code Playgroud)
如果我设置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
Run Code Online (Sandbox Code Playgroud)
我知道我遗漏了一些基本的东西,但我似乎找不到什么.
我没看到最初的错误.实际错误在于$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);
});
});
Run Code Online (Sandbox Code Playgroud)
对于未来我来说,这是谷歌的第一个结果.
寻找外部依赖!
Karma的日志有点误导,实际问题是主模块没有运行.例如,由Bower angular-stripe注入karma.conf.js的,需要加载实际的Stripe JS库,否则会崩溃整个应用程序(这本身就很烦人).我已将此行添加到karma.conf.js:
files: [
'https://js.stripe.com/v2',
Run Code Online (Sandbox Code Playgroud)
现在它的工作原理.
| 归档时间: |
|
| 查看次数: |
29106 次 |
| 最近记录: |