2014-01-11 95 views
1

我想爲我的<rating>組件(lib/rating/rating.dart/html)編寫單元測試(test/src/rating/rating.html/dart)。測試角度組件

void main() { 
    useHtmlEnhancedConfiguration(); 

    ng.Scope _rootScope; 
    dom.Element _element; 
    ngMock.TestBed _tb; 

    setUp(() { 
    try { 
    ngMock.setUpInjector(); 

     ngMock.module((ng.Module module) { 
     module.install(new RatingModule()); 
     }); 

     ngMock.inject((ng.Scope scope, ngMock.TestBed testBed) { 
     _rootScope = scope; 
     _rootScope['rate'] = 3; 
     _tb = testBed; 
     _element = _tb.compile('<rating value="rate"></rating>', scope: _rootScope); 
     _rootScope.$digest(); 

     }); 
     print('setUp done'); 
    } catch(e) { 
     print(e); 
    } 
    }); 

_element = _tb.compile('<rating value="rate>...失敗

[意想不到的請求:GET包/ bootstrap_angular /評級/ rating.html

我改變了組件templateUrl到模板,並指定模板HTML作爲字符串並擺脫了異常。

我做錯了什麼或者這是不是(支持)?

回答

2

我創建了issue in the Angular.dart GitHub repository。 隨着我得到的迴應,我認爲這是有效的,但相當冗長:

void main() { 
    useHtmlEnhancedConfiguration(); 

    ng.Scope _rootScope; 
    dom.Element _element; 
    ngMock.TestBed _tb; 
    ngMock.MockHttpBackend _backend; // new 

    setUp(() { 
    Future f; 
    try { 
    ngMock.setUpInjector(); 

     ngMock.module((ng.Module module) { 
     module.install(new RatingModule()); 
     }); 

     ngMock.inject((ng.Scope scope, ngMock.TestBed testBed) { 
     _rootScope = scope; 
     _rootScope['rate'] = 3; 
     _tb = testBed; 

     f = dom.HttpRequest.getString('/bootstrap_angular/packages/bootstrap_angular/rating/rating.html') // new 
     .then((ratingTemplate) { // new 
      _backend = new ngMock.MockHttpBackend(); // new 
      print(ratingTemplate); // just debug output 
      _backend.expect('GET').respond(ratingTemplate); // new 

      _element = _tb.compile('<rating value="rate"></rating>', scope: _rootScope); 
      var element =_element.shadowRoot.querySelector('i'); 
      _rootScope.$digest(); 
     }); 
     }); 
    } catch(e) { 
     print(e); 
    } 
    return f; // new 
    // return a future to make the tests wait until setUp is done 
    // found in this question 
    // How to wait for an asynchronous setup in a unit test, in Dart? 
    // http://stackoverflow.com/questions/14994518 
    });