检查指令是否评估特定的HTML元素?

时间:2014-09-12 13:55:26

标签: javascript angularjs angularjs-directive jasmine

我正在使用Yeoman来设置我的AngularJS项目。尝试使用Jasmine测试指令。该指令的计算结果为ul元素。我正在测试这是否成功。如何使用Jasmine编写语法?我正在关注Yo angular:directive生成的示例。我还想知道我是否以正确的方式接近这个?有没有正确学习Jasmine的指示?

除了下面的答案,我设法安装并使用jasmine-jquery来完成我的工作。

1 个答案:

答案 0 :(得分: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

关于Jasmine主页的教程实际上非常好http://jasmine.github.io/2.0/introduction.html