【问题标题】:cannot trigger action in a component when testing测试时无法触发组件中的操作
【发布时间】:2014-12-02 06:03:11
【问题描述】:

在我的测试中,我手动实例化一个组件并将其附加到 DOM。出于某种原因,我似乎无法使用单击操作触发该组件上的操作,但如果我要手动调用该操作的“发送”,那么它就可以工作。这是测试的 jsbin 链接:http://jsbin.com/copum/1/

        //THIS DOESN'T WORK
        click('button.click-me');
        andThen(function(){
            expect(find('#click-status').length).to.be(1);              
        }); 

        /*
        //THIS WORKS THOUGH
        Ember.run(function(){
            documentCollection.send('clickMe');
        });
        andThen(function(){
            expect(find('#click-status').length).to.be(1);              
        });             
        */

似乎 ember 出于某种原因无法找到与该操作关联的 DOM。有什么方法可以让点击事件起作用??

提前致谢。

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    您只是错过了测试顶部的访问 :)

    现在通过并按您预期的方式工作

        it("should show the text when clicking the button - using click", function() {
            visit('/');
            click('button.click-me');
            andThen(function(){
                expect(find('#click-status').length).to.be(1);              
            }); 
        });
    

    【讨论】:

    • 拍摄就是这样。感谢@Toran 解决这个问题。
    【解决方案2】:

    或者,如果您不想调用visit('/'),您可以手动创建一个Ember.EventDispatcher,只是不要忘记在完成后将其销毁。

    it("should show the text when clicking the button - using click", function() {
        dispatcher = Ember.EventDispatcher.create();
        dispatcher.setup();
        click('button.click-me');
        andThen(function(){
            expect(find('#click-status').length).to.be(1);              
            Ember.run(function(){
                dispatcher.destroy();
            });
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 2017-06-02
      相关资源
      最近更新 更多