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

时间:2019-03-20 20:59:54

标签: angular unit-testing dependency-injection ngoninit

我正在将Angular 7与Angular CLI版本7.3.4一起使用。我正在使用2个服务注入对组件进行单元测试,所有重要的代码都包含在下面。我包括了导致错误的存根,规范文件以及相关组件的重要部分。尽管如此,我仍然收到此错误“ TypeError:this.spreading.getSpreadingDays不是一个函数”,我知道夹具.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, useValue: AuthServiceStub },
        { provide: SpreadingService, useValue: SpreadingServiceStub }
      ],
      declarations: [ SpreadSignUpComponent ],
      schemas: [ NO_ERRORS_SCHEMA ],
    })
    .compileComponents();

    authService = TestBed.get(AuthService);
    spreadingService = TestBed.get(SpreadingService);
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(SpreadSignUpComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

存根类:

import { SpreadingService } from '../services/spreading.service';
import { of } from 'rxjs';
import { SpreadingDay } from 'src/app/shared/models/spreadingDay';
import { I } from 'src/app/shared/testing/interfaceBuilder';

export class SpreadingServiceStub implements I<SpreadingService> {

  getSpreadingDays() {
    return of([]);
  }

  getSpreadingDaysForUser() {
    return of([]);
  }

  setSpreadingDay(
    spreadingDay: SpreadingDay
  ) {
    return <Promise<void>>{};
  }

  addVolunteerToDay(
    spreadingDay: SpreadingDay,
    userID: string,
  ) {
    return <Promise<void>>{};
  }

  removeVolunteerFromDay(
    spreadingDay: SpreadingDay,
    userID: string,
  ) {
    return <Promise<void>>{};
  }

  addRequestToDay(
    spreadingDay: SpreadingDay,
    userID: string,
    first: string,
    last: string,
  ) {
    return <Promise<void>>{};
  }
}

组件:

import { Component, OnInit } from '@angular/core';
import { SpreadingService } from 'src/app/core/services/spreading.service';
import { SpreadingDayReturn, SpreadingDay } from 'src/app/shared/models/spreadingDay';
import { Observable } from 'rxjs';
import { AuthService } from 'src/app/core/services/auth.service';

@Component({
  selector: 'app-spread-sign-up',
  templateUrl: './spread-sign-up.component.html',
  styleUrls: ['./spread-sign-up.component.scss']
})
export class SpreadSignUpComponent implements OnInit {

  spreadingDays$: Observable<SpreadingDayReturn[]>;

  constructor(
    private auth: AuthService,
    private spreading: SpreadingService,
  ) { }

  ngOnInit() {
    this.spreadingDays$ = this.spreading.getSpreadingDays();
  }

1 个答案:

答案 0 :(得分:1)

您的代码{ provide: SpreadingService, useValue: SpreadingServiceStub }应该为{ provide: SpreadingService, useClass: SpreadingServiceStub }。很难注意到错误。我认为第二个服务存根应该应用相同的内容