我正在测试一个isolate指令,它作为控制器中使用的内部控制器和指令范围值。
这是来自真实代码的simplfied plunker http://plnkr.co/edit/r91xk1?p=preview,它是网格的指令包装器,但这显示了问题。
排除测试失败的原因:
beforeEach(inject(function($rootScope, $controller, $compile) {
$compile = $compile;
$scope = $rootScope.$new();
//Here is the scope information defined, what we want to get into the directive
$scope.itemToTest = "-beforeTest filled in valueFromTest--"
// Here we declare that the "itemToTest" attribute is bound to the *value* of itemToTest
elm = angular.element('<primary-grid itemToTest="itemToTest" ></primary-grid>');
e = $compile(elm)($scope);
$scope.$digest();
console.log("in beforeEach $scope %o ", $scope.$id);
}));
it('should have filled in the value the grid', function() {
// Access the isolateScope using <ELEMENT>.isolateScope, to see what's happening inside
//This fails
console.log("in Test e.scope() %o ", e.scope());
console.log("in Test e.isolateScope() %o ", e.isolateScope().$id);
console.log("in Test e.isolateScope().itemToTest %o ", e.isolateScope().itemToTest);
console.log("in Test e %o ", e);
//This Test fails
expect(e.isolateScope().itemToTest).toEqual($scope.itemToTest);
})
...
有关在测试中为指令设置正确值的最佳方法的任何想法,以便控制器可以使用它并且代码可以对单元进行测试吗?
感谢
答案 0 :(得分:0)
已解决:需要使用'item-to-test'作为标记中的属性,而不是camelCase(即JS)名称
使用:
elm = angular.element('<primary-grid item-to-test="itemToTest" ></primary-grid>’);
而不是:
elm = angular.element('<primary-grid itemToTest="itemToTest" ></primary-grid>’);