[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.
相關文章
相關標籤/搜索