EmberJs + Sinon Stub没有被正确的对象调用

EmberJs + Sinon Stub not called with proper object

本文关键字:对象 调用 Sinon Stub EmberJs      更新时间:2023-09-26

我有以下代码,测试是否正在调用适当的操作方法,但是随着设置,我似乎正在生成不同的控制器实例,因此测试失败。任何提示都会有帮助的,

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

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