Angular 6 Karma测试API问题
我不熟悉单元测试。我已经创建了一个Angular 6应用程序,希望进行单元测试。运行Angular 6 Karma测试API问题,angular,unit-testing,karma-jasmine,Angular,Unit Testing,Karma Jasmine,我不熟悉单元测试。我已经创建了一个Angular 6应用程序,希望进行单元测试。运行ng测试后,我遇到一些错误 通知页面正在调用服务NotificationService.getNotifications(),该服务在notification.service.ts 通知组件测试文件: import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { HttpClientModule } from
ng测试后,我遇到一些错误
通知页面正在调用服务NotificationService.getNotifications()
,该服务在notification.service.ts
通知组件测试文件:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { HttpClientModule } from '@angular/common/http';
import { NotificationsPageComponent } from './notifications-page.component';
import { RouterTestingModule } from "@angular/router/testing";
import { AuthService } from '../../shared/auth/auth.service';
describe('NotificationsPageComponent', () => {
let component: NotificationsPageComponent;
let fixture: ComponentFixture<NotificationsPageComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [AuthService],
declarations: [ NotificationsPageComponent ],
imports: [HttpClientModule, RouterTestingModule]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(NotificationsPageComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
此api\u链接
来自app config.service.ts
app config.service.ts
import { AppConfigService } from '../services/app-config.service';
import { AuthService } from '../shared/auth/auth.service';
....
export class NotificationService {
....
getNotifications() {
return this.http.get(this.appConfig.config.api_link + '/notes?show_on_dashboard=true', { headers: this.authService.header() })
.pipe(map(res => JSON.stringify(res)));
}
}
import { environment } from 'environments/environment';
export class AppConfigService {
private appConfig;
loadAppConfig() {
this.http.get('./environments/env_settings.json').subscribe((data) => {
this.appConfig = data;
console.log('Environment variables loaded from env_settings.json');
console.log(this.appConfig);
},
(err) => {
console.warn("Error loading env_settings.json, using envrionment file instead");
this.appConfig = environment;
});
}
get config() {
return this.appConfig;
}
}
我在Karma控制台中遇到6个错误,所有错误都是相同的类型错误:无法读取未定义的属性“api_link”
任何帮助都将不胜感激
感谢您在测试规范中未提供AppConfigService