2014-10-30 11 views
6

Arama sorgusundaki kullanıcı girdilerinin ayrıştırılması için bir test yazmaya çalışıyorum. fonksiyon bir Omurga adlı tanımlanır:Sinon fakeTimer ile Jasmine'de LoDash debounce nasıl test edilir?

SearchView = Backbone.View.extend({ 
    events: { 
     "input .search-input": "search" 
    }, 

    // init, render, etc. 

    search: _.debounce(function() { 
     this.collection.fetch(); 
    }, 200) 
}); 

Başlangıçta, omurga kitaplığı (v0.9.10) çizgi (v1.4.4) kullanılmıştır, ve test tanımlandı aşağıdaki gibi: Ancak

describe("SearchView", function() { 
    var view, $viewContainer; 

    beforeEach(function() { 
     appendSetFixtures('<div class="jasmine-container"></div>'); 
     $viewContainer = $(".jasmine-container"); 

     view = new SearchView({ 
      el: $viewContainer 
     }); 
    }); 

    afterEach(function() { 
     view.remove(); 
     view.cleanup(); 
    }); 

    //... 

    describe("wires the search input", function() { 
     var collectionStub, 
      fakeTimer; 

     beforeEach(function() { 
      collectionStub = sinon.stub(
       SearchResultsCollection.prototype, 
       "fetch" 
      );  

      fakeTimer = sinon.useFakeTimers(); 
     }); 

     afterEach(function() { 
      collectionStub.restore(); 
      fakeTimer.restore(); 
     }); 

     it("should not trigger a search before 200ms", function() { 
      fakeTimer.tick(199); 
      expect(collectionStub).not.toHaveBeenCalled(); 
     }); 

     it("should trigger a search after 200ms", function() { 
      fakeTimer.tick(200); 
      expect(collectionStub).toHaveBeenCalled(); 
     }); 
    }); 
}); 

şimdi, ben şimdi, altdizgi yerine LoDash'i dahil etmek istiyorum. Sitelerinde en son Underscore uyumluluk derlemesini kullanarak (LoDash 2.4.1/Underscore 1.5.6), tüm testlerim _.debounce!

Bazı araştırmalar yaptım ve runInContext ile bir LoDash Underscore oluşturma oluşturmak için bu relevantissues ile karşılaştım, ancak örnek olmaması nedeniyle nasıl kullanılacağını bilmiyorum. sinon.fakeTimer ile çalışmak üzere benim spec (s) 'da _.runInContext()'u nasıl kullanabilirim?

cevap

4
SearchView = Backbone.View.extend({ 
    events: { 
     "input .search-input": function() { 
      this.search(); 
     } 
    }, 

    initialize: function() { 
     this.search = _.debounce(this.search, 200); 
    } 

    // init, render, etc. 

    search: function() { 
     this.collection.fetch(); 
    } 
});  

describe("SearchView", function() { 
    var view; 
    var $viewContainer; 
    var clock; 
    var lodash = window._; 

    beforeEach(function() { 
     appendSetFixtures('<div class="jasmine-container"></div>'); 
     $viewContainer = $(".jasmine-container"); 

     clock = sinon.useFakeTimers(); 
     window._ = _.runInContext(window); 

     view = new SearchView({ 
      el: $viewContainer 
     }); 
    }); 

    afterEach(function() { 
     view.remove(); 
     view.cleanup(); 

     clock.restore(); 
     window._ = lodash; 
    }); 

    //... 

    describe("wires the search input", function() { 
     var collectionStub; 

     beforeEach(function() { 
      collectionStub = sinon.stub(
       SearchResultsCollection.prototype, 
       "fetch" 
      );  
     }); 

     afterEach(function() { 
      collectionStub.restore(); 
     }); 

     it("should not trigger a search before 200ms", function() { 
      fakeTimer.tick(199); 
      expect(collectionStub).not.toHaveBeenCalled(); 
     }); 

     it("should trigger a search after 200ms", function() { 
      fakeTimer.tick(200); 
      expect(collectionStub).toHaveBeenCalled(); 
     }); 
    }); 
}); 
+1

Üzgünüm, bunun için alamadım, ama' saati = sinon.useFakeTimers() ayarlamak; 've daha sonra başka bir 'fakeTimer.tick (199)' kullanın. Bir hata? – syabro

3

Sen SearchView veya _.debounce() herhangi çağrının (başlatmayla değil) yaratılmadan önce bu satırı

_ = _.runInContext(window); 

eklemek gerekir. Yani Lo-Dash dahil ettikten sonra doğru olmalıdır.

Bu, global pencere bağlamında lodash'u çalıştırmanıza olanak tanır, böylece SinonJSsetTimeout tarafından geçersiz kılmayı kullanabilirsiniz.

+0

Teşekkürler! Bunu bugün test edip öğleden sonra takip edeceğim. – SirTophamHatt

+0

'Sözdizimi hatası: "_" salt only' edilir –

+0

Muhtemelen' yerine 'arasında let' const' kullanın –

İlgili konular