【问题标题】:EmberJs + Sinon Stub not called with proper objectEmberJs + Sinon Stub 未使用正确的对象调用
【发布时间】:2014-08-04 20:51:20
【问题描述】:

我有以下代码来测试是否调用了正确的操作方法,但是通过设置我似乎生成了不同的控制器实例,因此测试失败。任何指针都会有所帮助,

module('SearchViewtests', {
setup : function() {

    App.reset();

    Ember.run(this, function(){

        this.controller = App.SearchController.create({
        });

        sinon.stub(this.controller._actions, "search");

        this.view = App.SearchView.create({
            controller : this.controller,
            container : App.__container__,
            context : this.controller
        });

        this.view.append();
    });
},
teardown : function(){
    Ember.run(this, function () {
        this.view.remove(); 
    });
    this.controller._actions.search.restore();
}});

test("on search button click invokes search action on controller", function(){

var button = this.view.$(".searchButton:button");

button.trigger('click');

ok(this.controller._actions.search.calledOnce, "Search page called");
});

【问题讨论】:

    标签: javascript unit-testing ember.js qunit sinon


    【解决方案1】:

    this 使用一个上下文,而不是设置this 和测试this

    test("on search button click invokes search action on controller",
      function(){
    
       var button = this.view.$(".searchButton:button");
    
       button.trigger('click');
    
       this.controller = App.SearchController.create({
        });
    
        sinon.stub(this.controller._actions, "search");
    
        this.view = App.SearchView.create({
            controller : this.controller,
            container : App.__container__,
            context : this.controller
        });
    
        this.view.append();
       ok(this.controller._actions.search.calledOnce, "Search page called");
    });
    

    【讨论】:

      猜你喜欢
      • 2019-09-26
      • 1970-01-01
      • 2019-07-16
      • 2019-10-28
      • 2018-07-19
      • 2012-07-04
      • 2015-12-11
      • 2021-09-04
      相关资源
      最近更新 更多