我正在测试一个隔离指令,该指令作为内部控制器和控制器中使用的指令范围值。
这是一个傻瓜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);
})
关于在测试中设置指令正确值的最佳方法是什么,以便控制器可以使用它,代码可以测试单元,有什么想法吗?
感谢
解决:需要使用"item to test"作为标记中的属性,而不是camelBase(即JS)名称
用途:
elm = angular.element('<primary-grid item-to-test="itemToTest" ></primary-grid>’);
而不是:
elm = angular.element('<primary-grid itemToTest="itemToTest" ></primary-grid>’);