我需要在不模拟后端的情况下测试Angular 4服务,但没有成功。
我的beforeEach如下:
import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { RouterStub } from './test-utils/router-stub';
import { MdSnackBarStub } from './test-utils/mdsnackbar-stub';
import { MdDialogStub } from './test-utils/mddialog-stub';
import { HttpModule, Http } from '@angular/http';
import { Router } from "@angular/router";
import { MdSnackBar, MdDialog } from "@angular/material";
import { AuthService } from './auth.service';
describe('AuthService', () => {
let authService: AuthService;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
HttpModule,
FormsModule
],
providers: [
AuthService,
{ provide: Router, useClass: RouterStub },
{ provide: MdSnackBar, useClass: MdSnackBarStub },
{ provide: MdDialog, useClass: MdDialogStub }
]
}).compileComponents().then((done) => {
this.authService = TestBed.get(AuthService);
this.authService.login('myUsername', 'myPassword');
});
}));
});
..但由于某种原因,Http无法真正调用API。 我知道该服务正在运行,因为手动测试时它是可以的。 我相信TestBed正在提供一个模拟的Http版本。
我想要这个,因为我需要真正记录AuthService以进行我需要的测试。 AuthService持有用户和其他数据的令牌信息。
可能有人给我指示或指出一些工作示例来测试Angular 4服务吗?
感谢。