Backbone.js的测试应用茉莉花 - 如何在一个视图测试模型绑定? [英] Testing backbone.js application with jasmine - how to test model bindings on a view?

查看:124
本文介绍了Backbone.js的测试应用茉莉花 - 如何在一个视图测试模型绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试的看法是否正确绑定事件有一些有趣的磨难。在骨干网,我们通常绑定到initialize方法的事件,用的东西沿着线: something.bind(变,this.render); 。在我的测试,我想确保此绑定设置,所以我做了以下内容:

I had some interesting tribulations in trying to test whether views were correctly bound to events.  In backbone, we typically bind to events in the initialize method, using something along the lines of: something.bind("change", this.render);.  In my test, I want to make sure that this binding is set up, so I did the following: 

this.myView = new MyView();
spyOn(this.myView, "render");;
this.legendView.groupData.trigger("change");
expect(this.legendView.render).toHaveBeenCalled();

但是,这是行不通的。因为在绑定MyView的的初始化函数时,事件获得的绑定到MyView的的渲染功能,在那个时候。所以,当你添加的间谍,它包装渲染功能,并将其设置回发生在myView.render。但是,第一绑定创建的闭包仍然存在,我们是完全hozed。所以,我们能做些什么呢?我所做的,就是我的移动电话绑定的一个单独的功能,是这样的:

But, that won't work.  Because the bind occurs in MyView's initialize function, the event get's bound to myView's render function AT THAT TIME.  So, when you add your spy, it wraps the render function and sets it back into place at myView.render.  But the closure created by the first bind still exists, and we are totally hozed.  So what can we do about it?  What I did, is move my bind call's to a seperate function, something like: 

myView = Backbone.View.extend({
initialize: function(){
    _.bindAll(this, "render");
    this.initialize_model_bindings();
},
initialize_model_bindings: function(){
    something.bind("change", this.render);
},
render: function(){ //... }
});

然后我的测试是这样的:

and my test then looks like:

this.myView = new MyView();
spyOn(this.myView, "render");
this.myView.initialize_model_bindings();
this.legendView.groupData.trigger("change");
expect(this.legendView.render).toHaveBeenCalled();

这工作,但我正在寻找一个更好的解决方案。谢谢

This works, but I'm looking for a better solution. Thanks

推荐答案

相反,刺探你可以尝试刺探something.bind回调。然后测试结合被称为瓦特/合适的参数。这是为我工作至今。我使用sinon.js而不是茉莉花内置的间谍。 sinon.js使得它更容易一点传递给方法调用同一方法调用堆栈ARGS测试(如一串电话到视图初始化绑定)。所以,我没有测试过这种想法W¯¯单独/茉莉,但相信这是可能的。

Instead of spying on the callback you might try spying on something.bind. Then test that bind was called w/ the appropriate arguments. This is working for me so far. I'm using sinon.js instead of jasmine's built-in spies. sinon.js makes it a bit easier to test for args passed to a method call in a stack of same method calls (eg a bunch of calls to bind in a view init). So I haven't tested this idea w/ jasmine alone but believe it should be possible.

spyOn(this.legendView.groupData, 'bind');
this.myView = new MyView();
expect(this.legendView.groupData.mostRecentCall.args).toEqual('change', this.myView.render); // example!! only works if testing a single call to bind or the last call in a series (ie mostRecentCall)

和W / sinon.js

And w/ sinon.js

sinon.spy(this.legendView.groupData, 'bind');
this.myView = new MyView();
expect(this.legendView.groupData.bind.calledWith('change', this.myView.render); // works w/ any number of calls to bind

这篇关于Backbone.js的测试应用茉莉花 - 如何在一个视图测试模型绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆