我正在使用Angular2测试版构建应用程序,当我运行我的测试用例时,我随机收到错误:
选择器" #root0"没有匹配任何元素'错误。
测试代码:
@Component({
template: '',
directives: [GroupBox, GroupBoxHeader]
})
class TestComponent {
}
describe('group-box control', () => {
it('should show the title', injectAsync([TestComponentBuilder], (tcb) => {
return tcb.overrideTemplate(TestComponent, '<group-box><group-box-header>Title Content</group-box-header></group-box>')
.createAsync(TestComponent).then( (fixture) => {
fixture.detectChanges();
let compiled = fixture.debugElement.nativeElement;
expect(compiled).toContainText('Title Content');
});
}));
it('should show the body', injectAsync([TestComponentBuilder], (tcb) => {
return tcb.overrideTemplate(TestComponent, '<group-box><group-box-header>Title Content</group-box-header>Body Content</group-box>')
.createAsync(TestComponent).then( (fixture) => {
fixture.detectChanges();
let compiled = fixture.debugElement.nativeElement;
expect(compiled).toContainText('Body Content');
});
}));
});
我正在使用Karma和Jasmine,遵循https://github.com/juliemr/ng2-test-seed
的指导原则以下存储库重现错误:https://github.com/cangosta/ng2_testing_controls
堆栈跟踪:
14 01 2016 11:40:32.073:INFO [业力]:Karma v0.13.19服务器始于http://localhost:9876/ 14 01 2016 11:40:32.078:INFO [启动器]:启动浏览器Chrome 14 01 2016 11:40:32.669:INFO [Chrome 47.0.2526(Windows 8.1 0.0.0)]:已连接到socket /#9hKZqsNbKFPfYWzIAAAA,ID为50428936 Chrome 47.0.2526(Windows 8.1 0.0.0)分组控件应显示正文FAILED 失败:选择器&#34; #root0&#34;与任何元素都不匹配 错误:选择器&#34; #root0&#34;与任何元素都不匹配 在新的BaseException(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2.dev.js:8080:21) 在DomRenderer_.createRootHostView(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2.dev.js:15248:15) 在AppViewManager_.createRootHostView(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2.dev.js:11265:52) 在C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2.dev.js:14531:46 在Zone.run(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:138:17) 在Zone.run(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2544:30) 在zoneBoundFn(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:111:19) 在lib $ es6 $ promise $$ internal $$ tryCatch(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1511:16) 在lib $ es6 $ promise $$ internal $$ invokeCallback(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1523:17) 在lib $ es6 $ promise $$ internal $$ publish(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1494:11) 在C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:243:5 在microtask(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2557:15) 在Zone.run(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:138:17) 在Zone.run(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2544:30) 在zoneBoundFn(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:111:19) 在lib $ es6 $ promise $ asap $$ flush(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1305:9) 失败:无法读取属性&#39; hostView&#39;未定义的 TypeError:无法读取属性&#39; hostView&#39;未定义的 在新的ComponentFixture_(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2047:97) 在C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2145:16 在Zone.run(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:138:17) 在Zone.run(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2544:30) 在zoneBoundFn(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:111:19) 在lib $ es6 $ promise $$ internal $$ tryCatch(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1511:16) 在lib $ es6 $ promise $$ internal $$ invokeCallback(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1523:17) 在lib $ es6 $ promise $$ internal $$ publish(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1494:11) 在C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:243:5 在microtask(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2557:15) 在Zone.run(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:138:17) 在Zone.run(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2544:30) 在zoneBoundFn(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:111:19) 在lib $ es6 $ promise $ asap $$ flush(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1305:9) 失败:无法读取属性&#39; detectChanges&#39;未定义的 TypeError:无法读取属性&#39; detectChanges&#39;未定义的 在C:/Projectos/Fontes/Stratus-HTML/build/test/infrastructure/controls/group-box.test.js:52:20 在Zone.run(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:138:17) 在Zone.run(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2544:30) 在zoneBoundFn(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:111:19) 在lib $ es6 $ promise $$ internal $$ tryCatch(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1511:16) 在lib $ es6 $ promise $$ internal $$ invokeCallback(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1523:17) 在lib $ es6 $ promise $$ internal $$ publish(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1494:11) 在C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:243:5 在microtask(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2557:15) 在Zone.run(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:138:17) 在Zone.run(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2544:30) 在zoneBoundFn(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:111:19) 在lib $ es6 $ promise $ asap $$ flush(C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1305:9) Chrome 47.0.2526(Windows 8.1 0.0.0):执行59 of 60(1 FAILED)(跳过1)(0.519秒/ 0.51秒) [11:40:33]&#39;测试&#39;在2.01秒之后出错 [11:40:33]错误:1 at formatError(C:\ Users \ LSANTOS \ AppData \ Roaming \ npm \ node_modules \ gulp \ bin \ gulp.js:169:10) 在Gulp。 (C:\用户\ LSANTOS \应用程序数据\漫游\ NPM \ node_modules \吞\ BIN \ gulp.js:195:15) 在emitOne(events.js:77:13) 在Gulp.emit(events.js:169:7) 在Gulp.Orchestrator._emitTaskDone(C:\ Projectos \ Fontes \ Stratus-HTML \ node_modules \ orchestrator \ index.js:264:8) 在C:\ Projectos \ Fontes \ Stratus-HTML \ node_modules \ orchestrator \ index.js:275:23 完成后(C:\ Projectos \ Fontes \ Stratus-HTML \ node_modules \ orchestrator \ lib \ runTask.js:21:8) 在cb(C:\ Projectos \ Fontes \ Stratus-HTML \ node_modules \ orchestrator \ lib \ runTask.js:29:3) 在removeAllListeners(C:\ Projectos \ Fontes \ Stratus-HTML \ node_modules \ karma \ lib \ server.js:333:7) 在服务器上。 (C:\ Projectos \丰特斯\ Stratus的-HTML \ node_modules \因缘\ lib中\ server.js:344:9) 在Server.g(events.js:260:16) 在emitNone(events.js:72:20) 在Server.emit(events.js:166:7) 在emitCloseNT(net.js:1518:8) at doNTCallback1(node.js:418:9) at process._tickCallback(node.js:340:17)
答案 0 :(得分:1)
这在Angular 2.0.0-beta.3中得到修复
有关详细信息,请参阅https://github.com/angular/angular/issues/6483#issuecomment-179557485
基本上,我必须做的事情:
- 使用tsd install jasmine -so手动添加jasmine的输入,并在测试文件中添加
///<reference...
;- 在我的导入中添加:
import {setBaseTestProviders} from 'angular2/testing';
import {
TEST_BROWSER_PLATFORM_PROVIDERS,
TEST_BROWSER_APPLICATION_PROVIDERS
} from 'angular2/platform/testing/browser';
在我的组件测试之前添加它:
setBaseTestProviders(TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS);