代码之家  ›  专栏  ›  技术社区  ›  JohnTheWayne

jquery命令在使用ember.js中的hbs渲染的测试中不起作用

  •  0
  • JohnTheWayne  · 技术社区  · 9 年前

    我一直在用Ember编写自己的组件测试。js 2.5.我目前正在测试按钮按下时的属性更改。测试可以正常运行,不会抛出错误,但测试中的jquery命令似乎不起作用。例如 this.$('button').click();

    我已经遵循了这里的指南, https://guides.emberjs.com/v2.5.0/testing/testing-components/ ,甚至一直在做他们所做的事情-相同的组件名称和所有内容,但仍然一无所获。没有抛出错误,jquery根本不起作用。

    由于组件测试使用htmlbars内联预编译插件来呈现组件,因此我确保遵循了该repo的所有说明。Babel已经更新,bower已经安装了所有必要的先决条件。

    这是按要求提供的测试代码

    test('it closes', function(assert) {
    
    this.render(hbs`{{modals/personal-information}}`);
    
    //making sure the property is set correctly before test
    this.set('isOpen', true);
    
    this.$('button').close();
    
    assert.equal('isOpen', false);
    
    }
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   JohnTheWayne    9 年前

    这在Kitler的帮助下得到了回答。

    我不是在渲染组件本身上设置属性,而是在测试上设置属性。一旦我了解了如何设置和检索渲染组件本身的属性,我的测试就完美地运行了。

    我遵循了这个指南, Ember component integration tests

    这是我的代码的一个新示例。

    test('it closes', function(assert) {
     assert.expect(1);
    
     this.set('open', true);
    
     this.render(hbs`{{modals/personal-information isOpen=open id="modal"}}`);
    
     var $button = this.$('#modal button');
    
     $button.click();
    
     assert.equal(this.get('open'), false);
    
    });