title: Testing Backbone View when collections fetch success callback calls render
body: <p>I am using Jasmine and Sinon to test my Backbone application but I am running in to some trouble. I am attempting to test that render gets called when my view is initialized</p>
  
  <p>I have the following View:</p>
  
  <pre><code>var MyView = Backbone.View.extend({
      el: '#myElement',
      initialize : function() {
          var that = this;
          this.collection.fetch({
              error : function() {
                  alert("error!");
              },
              success : function() {
                  that.render();
              }
          });
      },
  
      render : function() {
          this.collection.each(this.renderItem);
      }
  
      ...
  </code></pre>
  
  <p>My test</p>
  
  <pre><code>it('Should call render when view is instantiated', function(){          
                  spyOn(MyView.prototype, 'render');
                  var myCollection = new MyCollection();
                  this.view = new MyView({collection: myCollection});
                  expect(MyView.prototype.render).toHaveBeenCalled();
          });
  </code></pre>
  
  <p>The problem is that the expect() gets called before the success callback of my fetch has been executed. What is the best way to resolve this?</p>