[Unit Testing] Directive testing, isolated scope. 2
在测试文件中使用的scope是inject进去的,所以使用scope.clicked也可以找到原来directive定义的scope.
但是如果在driectvie中使用了isolated scope:
app.directive('testDir',function(){ return{ scope: {}, link: linkFn } function linkFn(scope, element){ element.addClass('panel'); element.bind("click",function(){ $scope.clicked = true; }) } })
我们再在测试文件中使用scope.clicked就会报错,这时候应该使用:
element.scope()
describe("testDir", function() { it("should add a class of panel", function() { expect(element.hasClass("panel")).toBe(false); }) it("should reponse click", function(){ browserGTrigger(element, "click") expect(element.scope().clicked).toBe(true); }) })
---------------------------------------------------
browserGTrigger 是在angular-scenario.js中定义的,
using the package https://npmjs.org/package/karma-ng-scenario fixed the problem for me. In short:
npm install karma-ng-scenario,
then add 'ng-scenario' to the 'frameworks' section in karma.conf.js.