2015-07-07 11 views
8

में element.find() वर्ग द्वारा उपयोग करते हुए जब यह परीक्षण चल रहा है, मैं त्रुटि Expected undefined to be true.कोणीय इकाई परीक्षण

it('should have the right classes', function() { 
    // this doesnt work 
    expect(element.find('.exampleClass').hasClass('ng-hide')).toBe(true); 

    // but this works 
    expect(element.find('span').hasClass('ng-hide')).toBe(true); 
}); 

मैं jqlite का उपयोग कैसे id और class द्वारा एक तत्व को खोजने के लिए कर सकते हैं बार आ रही है?

+0

आप नहीं कर सकते द्वारा लुकअप तक सीमित है। jqLite चयनकर्ताओं का समर्थन नहीं करता है – Phil

उत्तर

9

ऐसा इसलिए है क्योंकि कोणीय-जेक्लाइट (jquery की तुलना में बहुत हल्का लाइब्रेरी) find केवल टैग नामों को देखने के लिए सीमित है। आप element.querySelector(All) का उपयोग कर सकते हैं और इसे angular.element पर लपेट सकते हैं। यानी

var elm = element[0]; 
    expect(angular.element(elm.querySelector('.exampleClass')).hasClass('ng-hide')).toBe(true); 

    //Or even 
    expect(elm.querySelector('.exampleClass.ng-hide')).toBeDefined(); 

See documentation

खोज() - टैग नाम

संबंधित मुद्दे