gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 01:45:52 25 4
gpt4 key购买 nike

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

规范文件

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();
});
});

stub 类:
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();
}

最佳答案

您的代码 { provide: SpreadingService, useValue: SpreadingServiceStub }应该是 { provide: SpreadingService, useClass: SpreadingServiceStub } .很难注意到错误。我认为应该对第二个服务 stub 应用同样的方法

关于Angular 7 单元测试依赖注入(inject)在 ngOnInit 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55270100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com