2016-09-07 53 views
0

我試圖在一個限制爲屬性的Angular指令上運行幾個單元測試。現在我只是想確定指令是否存在。沒有模板的測試指令屬性

有誰知道如何測試指令,是一個屬性而已?

這裏是一個光禿禿的骨頭版本指令

function ResetCustomer() { 
    return { 
    restrict: 'A', 
    link: (scope, elem) => { 

     elem.bind('click', function() { 
     //do stuff 
     }); 
    } 
    }; 
} 

export default { 
    name: 'resetCustomer', 
    fn: ResetCustomer 
}; 

... ...的HTML

<a class="brand" reset-customer> 
    <img src="path/to/image.jpg"/> 
</a> 

...和測試

describe('Unit: ResetCustomer',() => { 

    let element, scope, compile; 

    beforeEach(() => { 
    angular.mock.module('app'); 
    angular.mock.inject(($compile, $rootScope) => { 
     scope = $rootScope.$new(); 
     compile = $compile; 
     element = angular.element('<a reset-customer></a>'); 
     compile(element)(scope); 
     scope.$digest(); 
    }); 
    }); 

    it('should exist',() => { 
    expect(element).toBeDefined(); 
    }); 
}); 
+0

看這裏:http://stackoverflow.com/questions/35570189/creating-single-custom-directive - 用於-NG-點擊即可執行,多控制器樂趣/ 35570797#35570797 我寫了有一個指令,不ES6但也許它會幫助你 – CodeNashor

+0

謝謝,但我正在尋找一種方法來測試指示。 – abyrne85

回答

1

您需要首先在指令上有可測試的東西。

例如在回調已它分配一個範圍可變isClicked到真/假。

然後你可以解僱的測試。點擊,並有isClicked應該是真實的斷言。

你甚至可以多走一英里,並有先前的斷言isClicked你火點擊之前是假的。我不能rmember什麼方法被調用,但90%肯定它的.trigger(「點擊」)或鼠標按下...