2014-06-11 11 views
6

के साथ डिबॉन्स फ़ंक्शन का उपयोग करके एंगुलरजेएस घड़ी का परीक्षण कैसे करें मेरे पास एक घड़ी वाला नियंत्रक है जो 500ms तक सूची फ़िल्टर करने में देरी से लॉसैश से बहस का उपयोग करता है।जैस्मीन

$scope.$watch('filter.keywords', _.debounce(function() { 
    $scope.$apply(function() { 
    $scope.filtered = _.where(list, filter); 
    }); 
}, 500)); 

मैं एक जैस्मीन परीक्षण simulates कि फ़िल्टर कीवर्ड कि जो कीवर्ड पाए जाते हैं, जिसके बाद पाया नहीं कर रहे हैं में प्रवेश लिखने की कोशिश कर रहा हूँ।

मेरा प्रारंभिक प्रयास कीवर्ड के लिए एक नया मूल्य निर्दिष्ट करने के बाद $ पाचन का उपयोग करना था, जो मुझे लगता है कि बहस के कारण काम नहीं किया गया था।

it('should filter list by reference', function() { 
    expect(scope.filtered).toContain(item); 
    scope.filter.keywords = 'rubbish'; 
    scope.$digest(); 
    expect(scope.filtered).not.toContain(item); 
    scope.filter.keywords = 'test'; 
    scope.$digest(); 
    expect(scope.filtered).toContain(item); 
}); 

तो मैंने $ टाइमआउट का उपयोग करने का प्रयास किया, लेकिन यह या तो काम नहीं करता है।

it('should filter list by reference', function() { 
    expect(scope.filtered).toContain(item); 
    $timeout(function() { 
    scope.filter.keywords = 'rubbish'; 
    }); 
    $timeout.flush(); 
    expect(scope.filtered).not.toContain(item); 
    $timeout(function() { 
    scope.filter.keywords = 'test'; 
    }); 
    $timeout.flush(); 
    expect(scope.filtered).toContain(item); 
}); 

मैं भी $ टाइमआउट 500ms debounce पर सेट से अधिक मूल्य देने की कोशिश की है।

दूसरों ने इस समस्या को हल कैसे किया है?

संपादित करें: मुझे एक समाधान मिला है जो $ टाइमआउट फ़ंक्शन में अपेक्षा को लपेटना था और फिर दायरे पर $ $ पर कॉल करना था।

it('should filter list by reference', function() { 
    expect(scope.filtered).toContain(item); 
    scope.filter.keywords = 'rubbish'; 
    $timeout(function() { 
    expect(scope.filtered).not.toContain(item); 
    }); 
    scope.$apply(); 
    scope.filter.keywords = 'test'; 
    $timeout(function() { 
    expect(scope.filtered).toContain(item); 
    }); 
    scope.$apply(); 
}); 

मैं अब भी जानना आवश्यक है कि इस दृष्टिकोण हालांकि सबसे अच्छा है इच्छुक हूँ।

उत्तर

4

यह एक बुरा दृष्टिकोण है। आपको एक कोणीय-विशिष्ट बहस का उपयोग करना चाहिए जैसे कि this जो सेटटाइमआउट के बजाय $ टाइमआउट का उपयोग करता है। इस तरह, आप

$timeout.flush(); 
    expect(scope.filtered).toContain(item); 

और स्पेक अपेक्षित के रूप में पास हो सकता है।

1

मैं का उपयोग किया है यह:

beforeEach(function() { 
    ... 
    spyOn(_, 'debounce').and.callFake(function (fn) { 
     return function() { 
      //stack the function (fn) code out of the current thread execution 
      //this would prevent $apply to be invoked inside the $digest 
      $timeout(fn); 
     };    
    }); 
}); 

function digest() { 
    //let the $watch be invoked 
    scope.$digest(); 
    //now run the debounced function 
    $timeout.flush(); 
} 

it('the test', function() { 
    scope.filter.keywords = ...; 
    digest(); 
    expect(...); 
}); 

आशा है कि यह मदद करता है