2014-09-12 43 views
0

我正在使用Yeoman來建立我的AngularJS項目。嘗試使用Jasmine測試指令。該指令的計算結果爲ul元素。我正在測試,看看這是否成功。如何使用Jasmine編寫語法?我遵循Yo angular:directive生成的示例。我也想知道我是否以正確的方式接近這一點?任何指針正確學習茉莉花?檢查指令是否評估爲特定的HTML元素?

除了下面的答案,我設法安裝和使用jasmine-jquery來完成我的工作。

回答

0

要測試你的指令,你可以編譯你的HTML模板在茉莉測試:

describe('Unit testing great quotes', function() { 
var $compile; 
var $rootScope; 

// Load the myApp module, which contains the directive 
beforeEach(module('myApp')); 

// Store references to $rootScope and $compile 
// so they are available to all tests in this describe block 
beforeEach(inject(function(_$compile_, _$rootScope_){ 
    // The injector unwraps the underscores (_) from around the parameter names when matching 
    $compile = _$compile_; 
    $rootScope = _$rootScope_; 
})); 

it('Replaces the element with the appropriate content', function() { 
    // Compile a piece of HTML containing the directive 
    var element = $compile("<your-directive></your-directive>")($rootScope); 

    // fire all the watches, so the scope expression {{1 + 1}} will be evaluated 
    $rootScope.$digest(); 

    // Check that the compiled element contains the templated content 
    expect(element.find("ul").length).toBeGreaterThan(1); 
}); 
}); 

https://docs.angularjs.org/guide/unit-testing

茉莉花主頁教程其實是非常好的http://jasmine.github.io/2.0/introduction.html

+0

非常感謝你!我設法通過使用jasmine-jquery來使用jQuery語法。但是你在這裏解釋了一些細節,這非常有幫助。 – 2014-09-16 08:16:04

相關問題