2015-06-24 113 views
1

我與sinon的fakeTimers有問題。這用於Marionette.js,下劃線和柴測試跑步者的環境中。如果我在chrome中設置了一個斷點並通過,我的計時器被觸發。但是,如果我沒有設置斷點,那麼它永遠不會被解僱。Sinon fakeTimers not firing

我正在使用_.debounce這是導致計時器設置。

這裏是如何的定時器設置:

describe('Basket item view', function() { 
    beforeEach(function() { 
    this.clock = sinon.useFakeTimers(); 

    this.app = new Backbone.Marionette.Application(); 
    this.app.addInitializer(basketInit); 
    this.app.start(basketOptions); 

    this.basketListView = this.app.baskets.currentView; 
    this.basketViews = this.basketListView.children; 
    }); 

    afterEach(function() { 
    this.app = this.basketListView = null; 
    this.clock.restore(); 
    }); 

    it('updates amount on quantity change', function() { 
    var basketLayoutView = this.basketViews.last(); 
    var itemView = basketLayoutView.basket.currentView.children.first(); 
    var items = basketLayoutView.collection; 
    var item = items.findWhere({id: 136}); 

    var $quantity = itemView.$('input.quantity'); 

    var updatedQuantity = 2; 
    var updatedPrice = '20.00'; 

    $quantity.val(updatedQuantity); 
    $quantity.change(); 

    this.clock.tick(500); 

    var newItemView = basketLayoutView.basket.currentView.children.first(); 
    var $amount = newItemView.$('td.amount.total'); 
    assert.equal(
     item.get('quantity'), updatedQuantity, 
     'quantity change'); 

    assert.equal(
     $amount.text().trim(), updatedPrice, 
     'amount change'); 
    }); 

回答

1

的問題是與Underscore.js和它的_.debounce功能。它試圖用比較日期來更準確,這很好,但是打破了與sinon的測試。

爲了解決這個問題,我只是重寫了防抖動功能與以前的版本:

_.debounce = function(func, wait, immediate) { 
    var timeout, result; 
    return function() { 
    var context = this, args = arguments; 
    var later = function() { 
     timeout = null; 
     if (!immediate) result = func.apply(context, args); 
    }; 
    var callNow = immediate && !timeout; 
    clearTimeout(timeout); 
    timeout = setTimeout(later, wait); 
    if (callNow) result = func.apply(context, args); 
    return result; 
    }; 
};