我有以下指令“krnGrid”的定义对象:
return {
restrict: 'E',
replace: true,
template: '<div></div>',
scope: {
'options': '=',
'gridModel': '=',
'stateModel': '='
},
link: function (scope, element, attrs, controller) {
var options = _getDefaultOptions();
options = angular.extend(options, scope.options);
options = optionsFactory.gatherOptions(scope, options, element, attrs);
var events = options.events;
delete options['events'];
angular.forEach(events, function (fn, name) {
element.bind(name, fn);
});
//watch configuration object for modifications
scope.$watch('gridModel.config', function (newvalue, oldvalue) {
if (newvalue !== oldvalue) {
//initialize grid with new configuration options
_initGrid(scope, element, newvalue.config, options);
}
}, true)
//watch data object for any data updates
scope.$watch('gridModel.data', function (newvalue, oldvalue) {
if (newvalue !== oldvalue) {
//update grid with new data
_reloadGrid(scope, element, newvalue.data);
}
}, true)
if (scope.gridModel.config.url && scope.gridModel.config.url != '') {
//load config data from server - TODO - this needs to be a configuration option - need to load data or not should be decided by user
//scope.gridModel.config.loadConfiguration();
}
if (scope.gridModel.data.url && scope.gridModel.data.url != '') {
//load config data from server - TODO - this needs to be a configuration option
//scope.gridModel.data.getData();
}
_initGrid(scope, element, scope.gridModel.config.config, options);
_reloadGrid(scope, element, scope.gridModel.data.data)
var gridObj = element.data().jqxGrid.instance
scope.stateModel.gridObj = gridObj;
}
};
我的单元测试如下:
define("angular-infra/tests/directives/GridDirectiveSpec", ['angular', 'jquery', 'i18n', 'angular-mocks', 'angular-infra/js/directives/Grid'],
function (angular, $) {
describe("Unit testing grid directive", function () {
var compile;
var scope;
beforeEach(module('html.infrastructure.components'));
beforeEach(inject(function ($compile, $rootScope) {
compile = $compile;
scope = $rootScope;
}));
it('Replaces the element with the appropriate content', function () {
scope.gridModel = {
config: {},
data: {}
};
var element = compile('<krn-grid gridModel="gridModel"></krn-grid>')(scope);
scope.$digest();
// Following assertion is intentional. Test never reaches this point
expect(true).toBe(true);
});
});
});
上述单元测试失败并显示以下错误消息:
Chrome 29.0.1547 (Windows 7) Unit testing grid directive Replaces the element with the appropriate content FAILED
TypeError: Cannot read property 'config' of undefined
at link (C:/Kronos/wfc/applications/navigator/html5/angular-infra/js/directives/Grid.js:266:24)
at nodeLinkFn (C:/Kronos/wfc/applications/navigator/html5/thirdparty/js/angular/angular.js:4222:13)
at compositeLinkFn (C:/Kronos/wfc/applications/navigator/html5/thirdparty/js/angular/angular.js:3834:14)
at C:/Kronos/wfc/applications/navigator/html5/thirdparty/js/angular/angular.js:3746:30
at null.<anonymous> (C:/Kronos/wfc/applications/navigator/html5/angular-infra/tests/directives/GridDirectiveSpec.js:73:81)
我无法弄清楚这个问题的原因。我清楚地在测试范围内设置了 gridModel 对象。
它与指令的隔离范围有关吗?如果是这样,我应该进行哪些更改来修复测试?