我正在使用yeoman angular-require
发电机。
https://github.com/aaronallport/generator-angular-require
我的一个控制器使用ng-table
. 当对这个控制器进行单元测试时,我收到错误:Unknown provider: ngTableProvider <- ngTable
.
我可以ng-table
在我的 webapp 中使用,没有任何问题。仅在单元测试用例中,我无法将其注入或添加为模块。请协助。
以下是我的设置:
ng-table 在 requirejs 配置路径中可用。
它也被添加到引导部分。
define(['angular', 'ng-table', 'controllers/main'], function (angular, ngTable, MainCtrl,) { return angular.module('myApp', [ 'ngTable', 'myApp.controllers.MainCtrl ', 'ngCookies', 'ngResource', 'ngSanitize', 'ngRoute' ]).config......
控制器:
define(['angular'], function (angular) {
'use strict';
angular.module('myApp.controllers.MainCtrl', [])
.controller('MainCtrl', function ($scope, ngTableParams) {
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'Karma'
];
$scope.tableParams = new ngTableParams({
page: 1,
count: 5
}, {
total: data.length,
getData: function($defer, params) {
$defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
});
});
测试设置:
ng-table
在 requirejs 配置路径中可用。
主要规范文件:
define(['angular', 'angular-mocks', 'ng-table', 'app'], function(angular, mocks, ngTable, app) {
'use strict';
describe('Controller: MainCtrl', function () {
// load the controller's module
beforeEach(module('scottsApp.controllers.MainCtrl'));
//Not sure how to inject ngTable
//tried beforeEach(module('ngTableParams'));
//tried beforeEach(module('ngTable'));
//tried beforeEach(inject(function () {}));
//Nothing is working....
var MainCtrl,
scope;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
MainCtrl = $controller('MainCtrl', {
$scope: scope
});
}));
it('should attach a list of awesomeThings to the scope to check controller load', function () {
expect(scope.awesomeThings.length).toBe(3);
});
});
});