title: How to stub Backbone View instantiated within another Views render method when Using RequireJS (and Jasmine / Sinon)
body: <p>I am attempting to write unit tests using Jasmine and Sion but I am struggling to find the equivalent of the following when using RequireJs to load modules:</p>
  
  <pre><code>sinon.stub(window, "MyItemView");
  </code></pre>
  
  <p>When using RequireJs, I am unable to stub this way as MyItemView is not attached to the window. </p>
  
  <p>The following is an example of when I need stub the MyItemView: </p>
  
  <pre><code>var MyView = Backbone.View.extend({
  el: '#myElement',
  initialize : function() {
      var that = this;
      this.collection.fetch({
          success : function() {
              that.render();
          }
      });
  },
  
  render : function() {
      this.collection.each(this.renderItem);
  }
  
  renderItem: function(model){
          var myItemView = new MyItemView({model: model});
          $('#myElement').append(myItemView.render().el);
      },
  
  ...
  </code></pre>
  
  <p>Now, using Jasmine I can test that the innerHtml contains the expected HTML:</p>
  
  <pre><code>it('View should contain correct Html', function(){
              var collectionFetchStub = sinon.stub(this.myCollection, 'fetch').yieldsTo('success', this.myCollection);
              this.view = new MyView({collection: this.myCollection});
              expect(this.view.el.innerHTML).toContain('&lt;li&gt;&lt;a href=""&gt; 1 : test &lt;/a&gt;&lt;/li&gt;');
  
              // Remove Stubs
              collectionFetchStub.restore();
          });
  </code></pre>
  
  <p>However, this test has a dependency on the rendering of MyItemView, which is not ideal for a unit test. What is the best solution to this problem? I am quite a novice to javascript and a solution to this seems complex.</p>