简体   繁体   English

Karma Server 下的 Angular 单元测试:未处理的承诺拒绝:', '代码应该在 fakeAsync 区域中运行

[英]Angular unit testing under Karma Server : Unhandled Promise rejection:', 'The code should be running in the fakeAsync zone

I'm using karma for my unit test developement.我正在使用 karma 进行单元测试开发。

I have used the couple fakeSynch - tick() for some test cases, also some promises.我已经将 fakeSynch - tick() 用于一些测试用例,还有一些承诺。

My tests are successfully running but it seems that something went wrong with the karma server:我的测试成功运行,但似乎 karma 服务器出现问题:

my test.spec.ts :我的 test.spec.ts :

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
import {RouterTestingModule} from '@angular/router/testing';
// Services and HTTP Module
import {HttpModule} from '@angular/http';
// Routs testing
import {Router, RouterModule} from '@angular/router';
import {routes} from './app-routing.module';
import {fakeAsync, tick} from '@angular/core/testing';
import {Location} from '@angular/common';
import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {AppRoutingModule} from './app-routing.module';

describe('Testing the application routes',() => {

  let location: Location;
  let router: Router;
  let fixture;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [ RouterTestingModule.withRoutes(routes)
        , FormsModule , DxTemplateModule , HttpModule ],
      providers: [SessionService , HttpService ],
      declarations: [
        AppComponent,
        LoginComponent,
        WelcomeComponent,
        ApplicationParametersComponent,
        InscriptionComponent,
        CustomersListComponent,
        CustomerDetailComponent
      ],
      schemas: [ CUSTOM_ELEMENTS_SCHEMA ]

    });
    router = TestBed.get(Router);
    location = TestBed.get(Location);
    fixture = TestBed.createComponent(AppComponent);
    router.initialNavigation();
  });

  afterEach(() => {
  });

  it('fakeAsync works', fakeAsync(() => {
    const promise = new Promise((resolve) => {
      setTimeout(resolve, 10);
    });
    let done = false;
    promise.then(() => done = true);
    tick(50);
    expect(done).toBeTruthy();
  }));

  it('navigate to nothing  takes you to /login', fakeAsync(() => {
    router.navigate(['']).then(() => {
      tick(50);
      expect(location.path()).toBe('/login');
    });
  }));
});

The output is this:输出是这样的: 在此处输入图片说明

The karma server logging error:业力服务器记录错误:

"C:\Program Files\nodejs\node.exe" "C:\Program Files\JetBrains\WebStorm 2017.2\plugins\js-karma\js_reporter\karma-intellij\lib\intellijRunner.js" --karmaPackageDir=D:\Sources\DotNet\NextGCClientWeb\node_modules\karma --serverPort=9876 --urlRoot=/
'Unhandled Promise rejection:', 'The code should be running in the fakeAsync zone to call this function', '; Zone:', 'ProxyZone', '; Task:', 'Promise.then', '; Value:', Error{__zone_symbol__currentTask: ZoneTask{_zone: Zone{_properties: ..., _parent: ..., _name: ..., _zoneDelegate: ...}, runCount: 0, _zoneDelegates: null, _state: 'notScheduled', type: 'microTask', source: 'Promise.then', data: undefined, scheduleFn: undefined, cancelFn: null, callback: function () { ... }, invoke: function () { ... }}}, 'Error: The code should be running in the fakeAsync zone to call this function
    at _getFakeAsyncZoneSpec (http://localhost:9876/base/src/test.ts?dc8091e99bff30029161c35e4b947de1fa70f0f8:34714:15)
    at Object.tick (http://localhost:9876/base/src/test.ts?dc8091e99bff30029161c35e4b947de1fa70f0f8:34732:5)
    at http://localhost:9876/base/src/test.ts?dc8091e99bff30029161c35e4b947de1fa70f0f8:144338:23
    at ZoneDelegate.invoke (http://localhost:9876/base/src/polyfills.ts?46bda4eeec1a82b89fa6336e969fb7275ac9e6b3:1546:26)
    at ProxyZoneSpec.Array.concat.ProxyZoneSpec.onInvoke (http://localhost:9876/base/src/test.ts?dc8091e99bff30029161c35e4b947de1fa70f0f8:232602:39)
    at ZoneDelegate.invoke (http://localhost:9876/base/src/polyfills.ts?46bda4eeec1a82b89fa6336e969fb7275ac9e6b3:1545:32)
    at Zone.run (http://localhost:9876/base/src/polyfills.ts?46bda4eeec1a82b89fa6336e969fb7275ac9e6b3:1296:43)
    at http://localhost:9876/base/src/polyfills.ts?46bda4eeec1a82b89fa6336e969fb7275ac9e6b3:1986:57
    at ZoneDelegate.invokeTask (http://localhost:9876/base/src/polyfills.ts?46bda4eeec1a82b89fa6336e969fb7275ac9e6b3:1579:31)
    at ProxyZoneSpec.Array.concat.ProxyZoneSpec.onInvokeTask (http://localhost:9876/base/src/test.ts?dc8091e99bff30029161c35e4b947de1fa70f0f8:232626:39)'

It seems that there is some trouble with the promises config, I wonder if that is affecting the quality of my tests (which seem to be running).似乎承诺配置有一些问题,我想知道这是否影响了我的测试(似乎正在运行)的质量。

Any ideas?有什么想法吗?

Try to wrap expect() with fakeAsync function尝试用fakeAsync函数包装expect()

remove it from it('navigate to nothing takes you to /login', fakeAsync(() => {从中删除它it('navigate to nothing takes you to /login', fakeAsync(() => {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的承诺拒绝: 区域:角度; 任务: - Unhandled Promise rejection: ; Zone: angular ; Task: 未处理的 Promise 拒绝与单元测试和 Angular - Unhandled Promise rejection with unit test and angular 当使用Promise和Observables进行单元测试代码时,fakeAsync和tick - fakeAsync and tick when unit testing code with Promise and Observables 未处理的承诺拒绝:AuthHttp没有提供者! ; 区域:angular2-jwt中的角度 - Unhandled Promise rejection: No provider for AuthHttp! ; Zone: angular in angular2-jwt 错误:代码应该在 fakeAsync 区域中运行以调用此 function - Error: The code should be running in the fakeAsync zone to call this function 角度2:未处理的承诺拒绝:没有AuthService的提供程序! ; 区域:角度 - Angular 2: Unhandled Promise rejection: No provider for AuthService! ; Zone: angular 未处理的承诺拒绝:this._next 不是函数:@angular/fire/messaging 中的区域 - Unhandled Promise rejection: this._next is not a function : Zone in @angular/fire/messaging 未处理的承诺拒绝:Zone.js 检测到 ZoneAwarePromise : Angular - Unhandled promise rejection: Zone.js has detected that ZoneAwarePromise : Angular 单元测试时如何解决控制台错误未处理的承诺拒绝? - How to resolve console error unhandled promise rejection when unit testing? Angular2 使用 fakeAsync 测试被拒绝的承诺 - Angular2 testing a rejected promise with fakeAsync
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM