Angular 角度业力-NullInjectorError:InjectionToken配置没有提供程序

Angular 角度业力-NullInjectorError:InjectionToken配置没有提供程序,angular,karma-jasmine,karma-coverage,Angular,Karma Jasmine,Karma Coverage,当我尝试测试时,它会显示错误 NullInjectorError: R3InjectorError(DynamicTestModule)[AlertService -> InjectionToken config -> InjectionToken config]: NullInjectorError: No provider for InjectionToken config! 这是我的组件 import { Component, OnInit } from '@angul

当我尝试测试时,它会显示错误

NullInjectorError: R3InjectorError(DynamicTestModule)[AlertService -> InjectionToken config -> InjectionToken config]: 
  NullInjectorError: No provider for InjectionToken config!
这是我的组件

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Validators, FormGroup, FormBuilder } from '@angular/forms';
import { ApiService } from '../services/api.service';
import { AlertService } from 'ngx-alerts';
import { NgxSpinnerService } from "ngx-spinner";
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';


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

  billingAddress: FormGroup;
  deliveryAreas: any;
  
  constructor(
    public router: Router,
    private formBuilder: FormBuilder,
    public apiService: ApiService,
    private alertService: AlertService,
    private spinner: NgxSpinnerService,
    private modalService: NgbModal) {
      this.spinner.show();

      this.billingAddress = this.formBuilder.group({
      'streetAddress': ['', Validators.compose([Validators.required])],
      'instructions': [null]
    });
    localStorage.setItem('val', '0');
    if(localStorage.getItem('recipientDetails')){
      this.router.navigate(['product']);
    } 
  }

  ngOnInit() {
    this.apiService.getDeliveryAreas().subscribe((res)=>{
      this.deliveryAreas = res.body.deliveryAreas;
      this.spinner.hide();
    });
  }

  get streetAddress() {
    return this.billingAddress.get('streetAddress');
  }



  address:any;

 
    
    this.address = {
      streetAddress : this.billingAddress.value.streetAddress,
    }

    this.apiService.getVendors(this.address.zipCode).subscribe((res)=>{
      if(res && res.body && res.body.error){
        this.alertService.danger(res.body.error);
        this.spinner.hide();
      }
    });
  }




}





检验。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。您的构造函数中有一个alertService,对于您的测试,您必须提供此服务:

beforeEach(async(() => {
    TestBed.configureTestingModule({
        providers: [
            { provide: AlertService, useClass: TestAlertService }
        ]
    }).compileComponents();
}));
此TestAlertService应为所有需要的请求提供模拟数据

--评论后编辑:--

TestAlertService应该具有函数danger,因为它在原始代码中用作函数。最终返回值的类型必须正确(布尔值、字符串等)


如果这还不够,请添加一段TestAlertService代码,以便我现在可以提供更具体的帮助

,显示“TypeError:this.alertService.danger不是函数”
class TestAlertService { 
  danger(value:string) {
  }
}