【发布时间】:2017-08-07 05:51:34
【问题描述】:
我正在编写一个基本测试来测试组件的初始化。就这样吧,
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 秒。 2017 年 7 月 8 日 15:33:56.115:WARN [网络服务器]: 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
我尝试将完整的MypackageModule 导入测试台,但我得到了同样的错误。关于它可能是什么的任何想法?
【问题讨论】:
标签: angular jasmine karma-runner karma-jasmine zone.js