Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在不模拟后端的情况下测试Angular 4服务_Angular_Unit Testing_Karma Jasmine_Testbed - Fatal编程技术网

在不模拟后端的情况下测试Angular 4服务

在不模拟后端的情况下测试Angular 4服务,angular,unit-testing,karma-jasmine,testbed,Angular,Unit Testing,Karma Jasmine,Testbed,我需要在没有模拟后端的情况下测试Angular 4服务,但没有成功 我的意见如下: import { ComponentFixture, TestBed, async } from '@angular/core/testing'; import { RouterStub } from './test-utils/router-stub'; import { MdSnackBarStub } from './test-utils/mdsnackbar-stub'; import { MdDial

我需要在没有模拟后端的情况下测试Angular 4服务,但没有成功

我的意见如下:

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正在保存用户和其他数据的令牌信息

有人可以给我一些指示或给我一些工作的例子来测试一个角度4服务只


谢谢。

开箱即用Angular CLI不配置任何模块模拟。检查您是否修改了
test.ts
文件或调用了
TestBed.configureTestingModule
,但没有撤消更改。实际上没有。我没动过test.ts。无论如何,谢谢。我会继续挖掘。为什么不模拟Auth服务并提供运行测试所需功能的实现呢?您只需使用函数创建一个新类MockAuth,并使用相同的
{provide:AuthService,useClass:MockAuth}