【问题标题】:Check if directive evaluates to a particular HTML element?检查指令是否评估为特定的 HTML 元素?
【发布时间】:2014-09-16 16:17:42
【问题描述】:

我正在使用 Yeoman 来设置我的 AngularJS 项目。尝试使用 Jasmine 测试指令。该指令的计算结果为 ul 元素。我正在测试这是否成功。如何使用 Jasmine 编写语法?我正在关注Yo angular:directive 生成的示例。我也想知道我是否以正确的方式处理这个问题?有什么正确学习茉莉花的建议吗?

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

【问题讨论】:

    标签: javascript angularjs angularjs-directive jasmine


    【解决方案1】:

    要测试你的指令,你可以在你的 jasmine 测试中编译你的 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

    【讨论】:

    • 非常感谢!我设法通过使用 jasmine-jquery 来使用 jQuery 语法。但是您在这里解释了一些细节,这非常有帮助。
    猜你喜欢
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 2011-10-26
    • 2012-03-13
    相关资源
    最近更新 更多