我尝试用Jasmine和Karma测试我的Ionic 2应用程序。当我运行"npm 测试"时,我有这个错误:
C:xampphtdocsMyApp>npm test
> ionic-hello-world@ test C:xampphtdocsMyApp
> karma start ./test-config/karma.conf.js
webpack: Compiled successfully.
webpack: Compiling...
ts-loader: Using typescript@2.0.9 and C:xampphtdocsMyApptsconfig.json
28 03 2017 14:24:17.950:WARN [karma]: No captured browser, open http://localhost:9876/
ERROR in ./src/pages/login/login.html
Module not found: Error: Can't resolve './assets/images/fr.svg' in 'C:xampphtdocsMyAppsrcpageslogin'
@ ./src/pages/login/login.html 1:1614-1647
@ ./src/pages/login/login.ts
@ ./src/app/app.component.ts
@ ./src/app/app.component.spec.ts
@ ./src .spec.ts
@ ./test-config/karma-test-shim.js
ERROR in ./src/pages/login/login.html
Module not found: Error: Can't resolve './assets/images/ch.svg' in 'C:xampphtdocsMyAppsrcpageslogin'
@ ./src/pages/login/login.html 1:1832-1865
@ ./src/pages/login/login.ts
@ ./src/app/app.component.ts
@ ./src/app/app.component.spec.ts
@ ./src .spec.ts
@ ./test-config/karma-test-shim.js
ERROR in ./src/pages/load-screen/load-screen.html
Module not found: Error: Can't resolve './assets/images/logo-yellow.svg' in 'C:xampphtdocsMyAppsrcpagesload-screen'
@ ./src/pages/load-screen/load-screen.html 1:94-136
@ ./src/pages/load-screen/load-screen.ts
@ ./src/pages/login/login.ts
@ ./src/app/app.component.ts
@ ./src/app/app.component.spec.ts
@ ./src .spec.ts
@ ./test-config/karma-test-shim.js
webpack: Failed to compile.
28 03 2017 14:24:17.974:INFO [karma]: Karma v1.5.0 server started at http://0.0.0.0:9876/
28 03 2017 14:24:17.975:INFO [launcher]: Launching browser Chrome with unlimited concurrency
28 03 2017 14:24:18.036:INFO [launcher]: Starting browser Chrome
28 03 2017 14:24:21.892:INFO [Chrome 56.0.2924 (Windows 10 0.0.0)]: Connected on socket Z7t5AaWcca3yDbsDAAAA with id 37780347
Chrome 56.0.2924 (Windows 10 0.0.0) ERROR
Uncaught Error: Cannot find module "./assets/images/logo-yellow.svg"
at webpack:///src/pages/load-screen/load-screen.html:1:0 <- karma-test-shim.js:150313
Chrome 56.0.2924 (Windows 10 0.0.0) ERROR
Uncaught Error: Cannot find module "./assets/images/logo-yellow.svg"
at webpack:///src/pages/load-screen/load-screen.html:1:0 <- karma-test-shim.js:150313
Chrome 56.0.2924 (Windows 10 0.0.0): Executed 0 of 0 ERROR (1.349 secs / 0 secs)
问题是我的项目中图像的路径。我不明白为什么,但它像模块一样体贴。
事实上,如果我删除项目中图像的所有路径,测试就会起作用。但是我需要保留我的图像...
在我的 html 中有一个图像路径的示例:
<ion-content padding>
<div class="loading-screen">
<img src="assets/images/logo-yellow.svg"><br><br>
<label>{{this.translate.get('mod-appFine-data_loading')}}</label><br>
<label>{{this.translate.get('mod-appFine-thanks_wait')}}</label><br>
<ion-spinner name="crescent" ></ion-spinner>
</div>
</ion-content>
有人有想法解决这个问题吗?
我的解决方案是在 var 中正确路径并在我的代码中调用此 var。它对我有用。