[Unit Testing] Directive testing, isolated scope. 2

在测试文件中使用的scope是inject进去的,因此使用scope.clicked也能够找到原来directive定义的scope.npm

可是若是在driectvie中使用了isolated scope:app

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);
        })
    })

 

---------------------------------------------------spa

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.
相关文章
相关标签/搜索