我刚刚开始用 Ionic 3 开发我的第一个应用程序,我现在正在进行一些单元测试。现在我面临一个问题,“编译”一个Service
when的单元测试Platform
是它的依赖项之一。
以下是摘要Service
:
@Injectable()
export class MyService {
constructor(public platform: Platform) {}
myMethod(): Observable<any> {
if (this.platform.is('android')) {
return new Observable(android);
else if (this.platform.is('ios')) {
return new Observable(iOS);
}
}
}
这是我按照互联网上的一些教程制作的单元测试:
describe('XXX', () => {
var service: MyService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [MyService, Platform]
});
});
it('should xxx', inject([MyService], (service) => {
var result = service.mymethod();
expect(result).toBeDefined();
}));
});
我的测试配置是在这篇博客文章之后进行的。我还有一些其他spec
文件可以成功运行,除了这个引发以下错误的文件:
PS C:\Workspaces\JS-TS\taccess> npm run test
> [email protected] test C:\Workspaces\JS-TS\taccess
> karma start ./test-config/karma.conf.js --single-run
(node:17164) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead
‼ 「wdm」:
i 「wdm」: Compiled with warnings.
i 「wdm」: Compiling...
‼ 「wdm」:
i 「wdm」: Compiled with warnings.
26 07 2018 07:20:43.963:INFO [karma]: Karma v2.0.5 server started at http://0.0.0.0:9876/
26 07 2018 07:20:44.003:INFO [launcher]: Launching browser Chrome with unlimited concurrency
26 07 2018 07:20:44.806:INFO [launcher]: Starting browser Chrome
26 07 2018 07:20:54.988:INFO [Chrome 68.0.3440 (Windows 10 0.0.0)]: Connected on socket u2KlYyff-qRa_fnHAAAA with id 6508927
Chrome 68.0.3440 (Windows 10 0.0.0) ERROR
{
"message": "An error was thrown in afterAll\nUncaught Error: Cannot find module './components/app/app-root'",
"str": "An error was thrown in afterAll\nUncaught Error: Cannot find module './components/app/app-root'"
}
Chrome 68.0.3440 (Windows 10 0.0.0) ERROR
{
"message": "An error was thrown in afterAll\nUncaught Error: Cannot find module './components/app/app-root'",
"str": "An error was thrown in afterAll\nUncaught Error: Cannot find module './components/app/app-root'"
}
Chrome 68.0.3440 (Windows 10 0.0.0): Executed 0 of 0 ERROR (0.008 secs / 0 secs)
Chrome 68.0.3440 (Windows 10 0.0.0): Executed 0 of 0 ERROR (0.142 secs / 0 secs)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: `karma start ./test-config/karma.conf.js --single-run`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\xxx\AppData\Roaming\npm-cache\_logs\2018-07-26T05_20_56_762Z-debug.log
为什么我需要注入Platform
服务?因为我使用的是BLE的 Cordova 插件,而且 iOS 和 Android 之间存在一些我需要管理的差异。
有没有人知道发生了什么?我究竟做错了什么?
注 1:我已经检查了删除依赖项,Platform
并且测试(由于缺少 进行了一些修改Platform
)工作得很好。
一段时间后,我发现了问题所在:Visual Studio Code。
出于某种原因,VSCode 更改了此导入
import { Platform } from 'ionic-angular';
对这个其他
import { Platform } from 'ionic-angular/umd';
一旦我删除了/umd
一切,就像一个魅力。
希望这可以帮助其他人在未来。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句