测试时无法触发组件中的操作

时间:2014-09-05 14:15:06

标签: ember.js

在我的测试中,我手动实例化一个组件并将其附加到DOM。出于某种原因,我似乎无法使用点击操作触发该组件上的操作,但如果我要手动调用“发送”#39;在行动然后它的工作原理。以下是测试的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。我可以用click事件做任何事情吗?

提前致谢。

2 个答案:

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

答案 1 :(得分:0)

或者,如果您不想拨打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();
        });
    });
});