我想为嵌套的控制器编写单元测试,但无法弄清楚如何在我的测试中模拟相同的行为。嵌套控制器的单元测试
我有2个控制器:
function FirstController ($scope) {
$scope.childs = [{
title : 'Hello, earth!'
}];
};
function SecondController ($scope) {
$scope.child.title = $scope.child.title + $scope.$index;
};
在我的HTML:
<div data-ng-controller="FirstController">
<div data-ng-repeat="child in childs" data-ng-controller="SecondController">
{{ child.title }}
</div>
</div>
而这个工作正常(http://jsfiddle.net/tcayp/1/)
的单元测试:
// FirstController
it('Should have childs', function() {
scope = {};
ctrl = new FirstController(scope);
expect(scope.childs.length).toBeGreaterThan(0);
});
// SecondController
it('Should have inherited a child', function() {
scope = {};
ctrl = new SecondController(scope);
expect(scope.child.title).toEqual('Hello, earth!0');
});
在SecondController-test我无法弄清楚如何模拟ng-repeat的继承链。
谢谢,我重写了它,以便它使用您的第一个示例。 – fredrik