1

我正在使用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>' + 
          '&nbsp;&nbsp;|&nbsp;&nbsp;' + 
          '<a href="http://someurl2.com/cookies" target="_blank">Cookies</a>' + 
          '&nbsp;&nbsp;|&nbsp;&nbsp;' + 
          '<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>' + 
       '&nbsp;&nbsp;|&nbsp;&nbsp;' + 
       '<a href="http://someurl2.com/cookies" target="_blank">Cookies</a>' + 
       '&nbsp;&nbsp;|&nbsp;&nbsp;' + 
       '<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); 
    }); 
}); 
+0

你嘗試過這麼遠嗎?如果至少有測試的基本代碼,則更容易提供幫助。 – dcodesmith 2015-03-19 09:20:14

+0

@dcodesmith - 添加了我正在嘗試的測試...試圖計算正確的數字 2015-03-19 09:25:07

回答

1

您應該注入加載角應用程序模塊,而不是指令。

describe('myDirective: ', function() { 
    var service, 
    scope, 
    compile, 
    element; 

    //load the angular app module 
    beforeEach(module('MyApp')); 

    beforeEach(inject(function ($rootScope, $compile) { 
     scope = $rootScope.$new(); 
     element = angular.element('<my-directive></my-directive>'); 
     $compile(element)(scope); 
     scope.$digest(); 
    })); 

    it('should have 3 anchor tags', function() { 
     expect(element[0].querySelectorAll('a').length).toEqual(3); 
    }); 

    it('should have a Home link', function() { 
     expect(element[0].querySelectorAll('a')[0].textContent).toEqual('Home'); 
    }); 

    // add more. Test for P tags. 

}); 

JSFFIDDLE

+0

謝謝,這有助於解決問題,但也幫助我拿起幾個問題與我的方法來測試 – 2015-03-19 11:21:10

+0

我想象我的方法是不同的這個:http://stackoverflow.com/questions/29142600/jasmine-how- to-test-a-stylesheet-directive,因爲我的指令是鏈接? – 2015-03-19 11:24:39

+1

其實並不重要,它仍然是一個指令,它有標籤。所以同樣的方法應該可以正常工作。 – dcodesmith 2015-03-19 11:27:18

1

在我看來,這是不值得寫這個指令的測試,因爲它不包含任何邏輯。

如果您真的想要測試它(例如,對您的應用程序或用戶來說,該元素與模板中的元素完全相同),您總是可以編寫測試來編譯指令並檢查它是否被正確替換通過它的模板。

在這種情況下,單元測試可以防止對模板進行任何不需要的更改。這不是單元測試的責任,但自動化用戶測試更適合這項工作。

+1

我受到同樣的印象。沒有任何邏輯的指令很難測試。我添加了一個我嘗試過的測試的基本示例,試圖計算正確的數字 2015-03-19 09:25:56

+0

@dcodesmith這是一個公平點。我已經重新回答了我的答案 – thomaux 2015-03-19 09:32:33