我正在使用 Jasmin、Simon 和 jasmin-simon 测试主干视图.
I'm testing a backbone view with Jasmin, Simon and jasmin-simon.
代码如下:
var MessageContainerView = Backbone.View.extend({
id: 'messages',
initialize: function() {
this.collection.bind('add', this.addMessage, this);
},
render: function( event ) {
this.collection.each(this.addMessage);
return this;
},
addMessage: function( message ) {
console.log('addMessage called', message);
var view = new MessageView({model: message});
$('#' + this.id).append(view.render().el);
}
});
实际上,我所有的测试都通过了,但只有一个.我想检查是否在向 this.collection
添加项目时调用了 addMessage
.
Actually, all my tests pass but one. I would like to check that addMessage
is called whenever I add an item to this.collection
.
describe('Message Container tests', function(){
beforeEach(function(){
this.messageView = new Backbone.View;
this.messageViewStub = sinon.stub(window, 'MessageView').returns(this.messageView);
this.message1 = new Backbone.Model({message: 'message1', type:'error'});
this.message2 = new Backbone.Model({message: 'message2', type:'success'});
this.messages = new Backbone.Collection([
this.message1, this.message2
]);
this.view = new MessageContainerView({ collection: this.messages });
this.view.render();
this.eventSpy = sinon.spy(this.view, 'addMessage');
this.renderSpy = sinon.spy(this.messageView, 'render');
setFixtures('<div id="messages"></div>');
});
afterEach(function(){
this.messageViewStub.restore();
this.eventSpy.restore();
});
it('check addMessage call', function(){
var message = new Backbone.Model({message: 'newmessage', type:'success'});
this.messages.add(message);
// TODO: this fails not being called at all
expect(this.view.addMessage).toHaveBeenCalledOnce();
// TODO: this fails similarly
expect(this.view.addMessage).toHaveBeenCalledWith(message, 'Expected to have been called with `message`');
// these pass
expect(this.messageView.render).toHaveBeenCalledOnce();
expect($('#messages').children().length).toEqual(1);
});
});
如您所见,确实调用了 addMessage
.(它会登录到控制台并按应有的方式调用 this.messageView
.在监视 addMessage
调用时我错过了什么?
As you can see addMessage
is called indeed. (It logs to the console and it calls this.messageView
as it should. What do I miss in spying for addMessage
calls?
谢谢,维克托
我不确定,但据我了解,会发生以下情况:
I'm not quit sure but, as I understand it, the following happens:
initialize
函数的新视图,并将您的 view.addMessage
绑定到您的集合.view.addMessage
这意味着你用一个监视函数覆盖它.这样做不会影响收集事件存储中存储的函数.initialize
function and bind your view.addMessage
to your collection. view.addMessage
which means you overwrite it with a spy function. Doing this will have no effect on the function that is stored in the collection event store.所以他们的测试存在一些问题.您的视图有很多您没有模拟出来的依赖项.您创建了一堆额外的 Backbone 模型和集合,这意味着您不仅要测试您的视图,还要测试 Backbones 集合和模型的功能.
So their are some problems with your test. You view has a lot of dependencies that you not mock out. You create a bunch of additional Backbone Models and Collections, which means you not test only your view but also Backbones Collection and Model functionality.
您不应该测试 collection.bind
是否可以工作,而是您已经使用参数 'add', this.addMessage 对集合调用了
bind
, 这个
You should not test that collection.bind
will work, but that you have called bind
on the collection with the parameters 'add', this.addMessage, this
initialize: function() {
//you dont
this.collection.bind('add', this.addMessage, this);
},
所以,模拟集合很容易:
So, its easy to mock the collection:
var messages = {bind:function(){}, each:function(){}}
spyOn(messages, 'bind');
spyOn(messages, 'each');
this.view = new MessageContainerView({ collection: messages });
expect(message.bind).toHaveBeenCalledWith('bind', this.view.addMessage, this.view);
this.view.render()
expect(message.each).toHaveBeenCalledWith(this.view.addMessage);
... and so on
这样做你只测试你的代码,而不依赖于 Backbone.
Doing it this way you test only your code and have not dependencies to Backbone.
这篇关于Sinon 似乎没有监视事件处理程序回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!