I execute a method in the Backbone’s View initialize method.
initialize : function(options) {
this.myMethod();
}
I am trying to spy on this method using sinon like:
this.spyMyMethod = sinon.spy(this.view, "myMethod");
end then
it('should call my method', function(){
expect(this.spyMyMethod).toHaveBeenCalledOnce();
});
but the test fails…
Any ideas?
You are spying on the method too late.
Wherever you are assigning
this.viewI assume it is from a call likenew Views.SomeView(). It is thatnewcall that will make theinitializefunction be executed.Update
I don’t really recommend doing this because it is pretty messy, but you can possibly do something like the following: (I don’t know sinon but this is how you would do it with the base jasmine spy objects)
Another Possiblilty
Looks like it might be possible to override that method with a spy like below. If that works, it is probably the cleanest way to do this.