小编Cal*_*rne的帖子

Angular 7 单元测试依赖注入在 ngOnInit 中不起作用

我正在使用 Angular 7 和 Angular CLI 版本 7.3.4。我正在对一个具有 2 个服务注入的组件进行单元测试,所有重要代码都包含在下面。我包含了导致错误的存根、规范文件以及相关组件的重要部分。尽管如此,我仍然收到此错误“TypeError:this.spreading.getSpreadingDays不是函数”我知道fixture.detectchanges()是导致ngOnInit被调用的原因,但应该定义该函数,因为我注入了服务。我确信这是一个愚蠢的错误,但我看过很多教程,但我无法弄清楚我的代码出了什么问题。任何建议,将不胜感激。

规格文件

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { SpreadSignUpComponent } from './spread-sign-up.component';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { AuthService } from 'src/app/core/services/auth.service';
import { SpreadingService } from 'src/app/core/services/spreading.service';
import { AuthServiceStub } from 'src/app/core/testing/auth.service.stub';
import { SpreadingServiceStub } from 'src/app/core/testing/spreading.service.stub';

describe('SpreadSignUpComponent', () => {
  let component: SpreadSignUpComponent;
  let fixture: ComponentFixture<SpreadSignUpComponent>;

  let authService: AuthService;
  let spreadingService: SpreadingService;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      providers: [
        { provide: AuthService, …
Run Code Online (Sandbox Code Playgroud)

unit-testing dependency-injection typescript ngoninit angular

4
推荐指数
1
解决办法
3520
查看次数