EmberJs + Sinon Stub没有用适当的对象调用

时间:2014-06-14 12:14:40

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

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

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");
});

1 个答案:

答案 0 :(得分:0)

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");
});