我正在使用Jasmine來測試我的Angular組件。我能夠但是測試功能,服務和控制器以及,如何測試一個只包含模板的指令,例如:如何測試僅用作模板的指令
app.directive('myDirective', function() {
return {
restrict: 'E',
replace: true,
transclude: true,
template: '<header>' +
'<p>' +
'<a href="http://someurl1.com/home" target="_blank">Home</a>' +
' | ' +
'<a href="http://someurl2.com/cookies" target="_blank">Cookies</a>' +
' | ' +
'<a href="http://someurl3.com/Terms" target="_blank">Terms</a>' +
'</p>' +
'<p>Text text text text text text text text text text text text text text text text text</p>' +
'</header>'
};
});
我沿着這些路線嘗試的東西:
describe('Directive: header', function() {
beforeEach(module('headerDirective'));
var element, scope;
beforeEach(inject(function ($rootScope, $compile) {
element = angular.element('<header>' +
'<p>' +
'<a href="http://someurl1.com/home" target="_blank">Home</a>' +
' | ' +
'<a href="http://someurl2.com/cookies" target="_blank">Cookies</a>' +
' | ' +
'<a href="http://someurl3.com/Terms" target="_blank">Terms</a>' +
'</p>' +
'<p>Text text text text text text text text text text text text text text text text text</p>' +
'</header>');
scope = $rootScope;
$compile(element)(scope);
scope.$digest();
}));
it("should have the correct amount of <a>", function() {
var list = element.find('a');
expect(list.length).toBe(a);
});
});
你嘗試過這麼遠嗎?如果至少有測試的基本代碼,則更容易提供幫助。 – dcodesmith 2015-03-19 09:20:14
@dcodesmith - 添加了我正在嘗試的測試...試圖計算正確的數字 – 2015-03-19 09:25:07
當您運行測試時會發生什麼? – dcodesmith 2015-03-19 09:38:24