gpt4 book ai didi

dart - 测试角度分量

转载 作者:行者123 更新时间:2023-12-03 03:28:41 26 4
gpt4 key购买 nike

我想为test/src/rating/rating.html/dart组件(<rating>)编写单元测试(lib/rating/rating.dart/html)。

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>...失败,并带有

[Unexpected request: GET packages/bootstrap_angular/rating/rating.html



我将组件templateUrl更改为template,并将模板HTML分配为字符串,并摆脱了异常。

我做错什么了吗?还是不支持?

最佳答案

我创建了一个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
});

关于dart - 测试角度分量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21061382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com