单元测试时无法访问指令中的模型(函数)
Cant access Model (function) in Directive while unit testing
单元测试使用 ngModel 的指令,该指令具有多个函数,包括 getAll()。模型被完美注入(当我输出它时,它显示可访问 getters/setters/etc)。我将它传递给元素。做一个编译和摘要。
虽然收到错误“类型错误:无法读取未定义的 属性 'getAll'”。
'console.log('vehiclesModel', vehiclesModel.get('vehicles'));'
输出存根数据!
'use strict';
describe('Directive: selectBox', function () {
beforeEach(module('sytacApp'));
beforeEach(module('syt.templates'));
var scope,
httpBackend,
$rootScope,
$compile,
element,
vehiclesModel,
stubbedData;
beforeEach(function () {
inject(function ($injector) {
$compile = $injector.get('$compile');
});
});
beforeEach(inject(function (_$rootScope_, _$httpBackend_, _vehiclesModel_, _stubbedData_) {
httpBackend = _$httpBackend_;
$rootScope = _$rootScope_;
vehiclesModel = _vehiclesModel_;
stubbedData = _stubbedData_;
vehiclesModel.set('vehicles', {data: stubbedData.container});
console.log('vehiclesModel', vehiclesModel.get('vehicles'));
}));
it('should process model data accordingly', function () {
var element = angular.element('<select-box identifier="type" selectedidentifier="selectedType" model="vehiclesTypesModel" data-ng-model="vehiclesModel"></select-box>');
element = $compile(element)(scope);
scope.$digest();
//......
});
});
问题。我是不是忽略了什么?
必须在'$compile'
之前将'vehiclesModel'放在'范围scope.vehiclesModel'上
单元测试使用 ngModel 的指令,该指令具有多个函数,包括 getAll()。模型被完美注入(当我输出它时,它显示可访问 getters/setters/etc)。我将它传递给元素。做一个编译和摘要。
虽然收到错误“类型错误:无法读取未定义的 属性 'getAll'”。
'console.log('vehiclesModel', vehiclesModel.get('vehicles'));'
输出存根数据!
'use strict';
describe('Directive: selectBox', function () {
beforeEach(module('sytacApp'));
beforeEach(module('syt.templates'));
var scope,
httpBackend,
$rootScope,
$compile,
element,
vehiclesModel,
stubbedData;
beforeEach(function () {
inject(function ($injector) {
$compile = $injector.get('$compile');
});
});
beforeEach(inject(function (_$rootScope_, _$httpBackend_, _vehiclesModel_, _stubbedData_) {
httpBackend = _$httpBackend_;
$rootScope = _$rootScope_;
vehiclesModel = _vehiclesModel_;
stubbedData = _stubbedData_;
vehiclesModel.set('vehicles', {data: stubbedData.container});
console.log('vehiclesModel', vehiclesModel.get('vehicles'));
}));
it('should process model data accordingly', function () {
var element = angular.element('<select-box identifier="type" selectedidentifier="selectedType" model="vehiclesTypesModel" data-ng-model="vehiclesModel"></select-box>');
element = $compile(element)(scope);
scope.$digest();
//......
});
});
问题。我是不是忽略了什么?
必须在'$compile'
之前将'vehiclesModel'放在'范围scope.vehiclesModel'上