我想单元测试一个双向绑定属性(=)的指令。该指令在我的应用程序中起作用,但我无法获得测试双向绑定的单元测试工作。angularjs指令单元测试失败与controllerAs,bindToController&isolateScope()
我一直在试图让这工作几天。我已经阅读了许多使用一些但不是全部我想使用的功能的示例:controllerAs,bindToController,以及isolateScope()。 (忘记templateURL,我也需要添加,如果我能得到这个工作!:)
我希望有人可以告诉我如何显示隔离范围内反映的父范围的变化。
这里是一个包含下面的代码plunkr:
http://plnkr.co/edit/JQl9fB5kTt1CPtZymwhI
这里是我的测试程序:
var app = angular.module('myApp', []);
angular.module('myApp').controller('greetingController', greetingController);
greetingController.$inject = ['$scope'];
function greetingController($scope) {
// this controller intentionally left blank (for testing purposes)
}
angular.module('myApp').directive('greetingDirective',
function() {
return {
scope: {testprop: '='},
restrict: 'E',
template: '<div>Greetings!</div>',
controller: 'greetingController',
controllerAs: 'greetingController',
bindToController: true
};
}
);
这里是规格:
describe('greetingController', function() {
var ctrl, scope, rootScope, controller, data, template,
compile, isolatedScope, element;
beforeEach(module('myApp'));
beforeEach(inject(function ($injector) {
rootScope = $injector.get('$rootScope');
scope = rootScope.$new();
controller = $injector.get('$controller');
compile = $injector.get('$compile');
data = {
testprop: 1
};
ctrl = controller('greetingController', {$scope: scope}, data);
element = angular.element('<greeting-directive testprop="testprop"></greeting-directive>');
template = compile(element)(scope);
scope.$digest();
isolatedScope = element.isolateScope();
}));
// PASSES
it('testprop inital value should be 1', function() {
expect(ctrl.testprop).toBe(1);
});
// FAILS: why doesn't changing this isolateScope value
// also change the controller value for this two-way bound property?
it('testprop changed value should be 2', function() {
isolatedScope.testprop = 2;
expect(ctrl.testprop).toBe(2);
});
});
谢谢你非常感谢你的善良和专业知识。这对我非常有帮助。谢谢!愿你在各个层面获得丰盛的财富。 – DanBKC
@DanBKC嘿,感谢队友祝福,多年来我一直在帮助其他人。但是,你是这样一个好评的人,谢谢;)它鼓励我做更多的贡献:) –