Angular 4 - 测试设置错误

时间:2017-08-07 05:51:34

标签: angular jasmine karma-runner karma-jasmine zone.js

我正在编写一个基本测试来测试组件的初始化。在这里,

import { NO_ERRORS_SCHEMA } from '@angular/core';
import {
  inject,
  async,
  TestBed,
  ComponentFixture,
} from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

import { AppInsightsModule, AppInsightsService } from 'ng2-appinsights';
import { FredAuthAdalModule, AuthService } from '@MyPackage';

/**
 * Load the implementations that should be tested
 */
import { AppComponent } from './app.component';
import { AppState } from './app.service';

class MockAuthService extends AuthService {
}

describe(`App`, () => {
  let comp: AppComponent;
  let fixture: ComponentFixture<AppComponent>;

  /**
   * async beforeEach
   */
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [RouterTestingModule, AppInsightsModule, FredAuthAdalModule],
      declarations: [AppComponent],
      schemas: [NO_ERRORS_SCHEMA],
      providers: [AppState, AppInsightsService, { provide: AuthService, useClass: MockAuthService }]
    })
      /**
       * Compile template and css
       */
      .compileComponents();
  }));

  /**
   * Synchronous beforeEach
   */
  beforeEach(() => {
    fixture = TestBed.createComponent(AppComponent);
    comp = fixture.componentInstance;

    /**
     * Trigger initial data binding
     */
    fixture.detectChanges();
  });

  it(`should be initialized`, () => {
    expect(fixture).toBeDefined();
    expect(comp).toBeDefined();
  });

});

我已经模拟了AuthService,因为它具有多级依赖性。将AuthService提供给测试台会导致错误“找不到AuthInternalDependency的提供者”。

  

[at-loader]好的,0.118秒。       07 08 2017 15:33:56.115:WARN [web-server]:404:/env.json       Chrome 60.0.3112(Windows 10 0.0.0)错误         未捕获的SyntaxError:位于0的JSON中的意外标记N.         在webpack:///node_modules/zone.js/dist/zone.js:195:0&lt; - config / spec-bundle.js:78354       Chrome 60.0.3112(Windows 10 0.0.0)错误         未捕获的SyntaxError:位于0的JSON中的意外标记N.         在webpack:///node_modules/zone.js/dist/zone.js:195:0&lt; - config / spec-bundle.js:78354

我尝试将完整的MypackageModule导入测试台,但我得到了同样的错误。关于它可能是什么的任何想法?

1 个答案:

答案 0 :(得分:0)

能否请您删除expect(fixture).toBeDefined();