在 Ionic 服务的单元测试中注入 <Platform> 时抛出的错误

卡兹贝尔

我刚刚开始用 Ionic 3 开发我的第一个应用程序,我现在正在进行一些单元测试。现在我面临一个问题,“编译”一个Servicewhen的单元测试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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在单元测试AngularJS服务时注入依赖服务

在对子类进行单元测试时,如何在抽象类中注入变量?

单元测试指令时,Angular无法注入定制服务

测试服务时无法在服务中注入$ q

离子| “ TypeError:运行单元测试时,this.platform.is不是函数

Ionic 3中的Android SDK Platform 26许可错误

错误:无法找到凉亭组件“ ionic-platform-web-client”

在Angular 2(Ionic 2 / Angular 2 / Typescript)的服务中注入服务时如何传递依赖关系的参数

单元测试安心时@Context对象未注入

在 ASP .Net Core 构造函数中注入 Amazon S3 服务时出现“调用目标抛出异常”错误

Ionic 2 ViewController单元测试

Ionic 2 NavController单元测试

错误:将linkedin插件添加到Ionic3时,包com.linkedin.platform不存在

单元测试抛出异常

单元测试抛出NullReferenceException

如何为 Material UI 滑块编写单元测试?尝试呈现组件时抛出“错误:无法读取 null 的属性‘addEventListener’”

具有模拟服务的单元测试组件-错误

关于 http 服务错误的 angular 6 单元测试

MatDialog服务单元测试Angular 6错误

在单元测试Angular服务时模拟AngularFireAuth

监视服务方法时,Jasmine单元测试失败

在Angular 2的构造函数中注入了Router的麻烦的单元测试服务

AngularJS控制器在注入工厂时抛出错误

IONIC Serve:运行时错误platform.toLowerCase不是函数

测试node.crypto函数时,mocha抛出错误

测试Sage Evolution节点连接时抛出Droppoint错误

linst Jest测试文件时,eslint抛出“ no-undef”错误

在运行测试用例ClientHttpResponse时抛出401错误

错误:在控制器中注入服务时,[$ injector:unpr]