Ember.js 如何测试连接承诺中的逻辑/then
我正在尝试为我的一个组件上的操作处理程序编写测试。我正在我的一个模型上删除Ember.js 如何测试连接承诺中的逻辑/then,ember.js,Ember.js,我正在尝试为我的一个组件上的操作处理程序编写测试。我正在我的一个模型上删除save方法,以使用Em.RSVP.promise.resolve() 在我的组件中,我使用然后: 返回目标 .save() .然后(函数(){ 选中。回滚(); 此.sendAction('quicklinkChanged',target); }.bind(this),this.notify_user_关于_persistence_error.bind(this,'另存为') 这是我在服务器端使用的一种模式,我们在pro
save
方法,以使用Em.RSVP.promise.resolve()
在我的组件中,我使用然后:
返回目标
.save()
.然后(函数(){
选中。回滚();
此.sendAction('quicklinkChanged',target);
}.bind(this),this.notify_user_关于_persistence_error.bind(this,'另存为')代码>
这是我在服务器端使用的一种模式,我们在promise库中使用when
。但是,当我在客户端执行此操作时,我永远不会在then
块中的函数中结束,因此我无法在单元测试中断言其中的任何功能
有人能提供关于实现这一点的最佳方法的见解吗?我们将回调移出了该方法,这样我们可以单独调用它们并验证其功能,或者替换它们并验证它们是否被调用
控制器示例:
测验
例如:
App.IndexController = Em.Controller.extend({
randomProperty: 1,
async: function(fail){
return new Em.RSVP.Promise(function(resolve, reject){
if(fail){
reject('fdas');
}else{
resolve('foo');
}
});
},
doAsyncThing: function(fail){
return this.async(fail).then(this.success.bind(this), this.failure.bind(this));
},
success: function(){
this.set('randomProperty', 2);
},
failure: function(){
this.set('randomProperty', -2);
}
});
test("async success", function(){
var ic = App.IndexController.createWithMixins();
stop();
ic.doAsyncThing(false).then(function(){
start();
equal(ic.get('randomProperty'), 2);
});
});
test("async fail", function(){
var ic = App.IndexController.createWithMixins();
stop();
ic.doAsyncThing(true).then(function(){
start();
equal(ic.get('randomProperty'), -2);
});
});
test("async success is called", function(){
expect(1);
var ic = App.IndexController.createWithMixins();
ic.success = function(){
ok(true);
};
stop();
ic.doAsyncThing(false).then(function(){
start();
});
});
test("async failure is called", function(){
expect(1);
var ic = App.IndexController.createWithMixins();
ic.failure = function(){
ok(true);
};
stop();
ic.doAsyncThing(true).then(function(){
start();
});
});
test("doAsyncThing returns a promise", function(){
expect(1);
var ic = App.IndexController.createWithMixins();
ok(ic.doAsyncThing(true).then);
});