我正在尝试为嵌套的控制器编写单元测试,但无法弄清楚如何在我的测试中模拟相同的行为。
我有 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 模拟继承链。