【问题标题】:angular karma - NullInjectorError: No provider for InjectionToken config角度业力 - NullInjectorError:InjectionToken 配置没有提供者
【发布时间】:2020-11-14 06:28:56
【问题描述】:

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

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




}




.................................................. ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... .....................

【问题讨论】:

    标签: angular karma-jasmine karma-coverage


    【解决方案1】:

    您的构造函数中有一个 alertService,为了进行测试,您必须提供此服务:

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            providers: [
                { provide: AlertService, useClass: TestAlertService }
            ]
        }).compileComponents();
    }));
    

    这个 TestAlertService 应该为所有需要的请求提供模拟数据。

    -- 评论后编辑:--

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

    class TestAlertService { 
      danger(value:string) {
      }
    }
    

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

    【讨论】:

      猜你喜欢
      • 2020-06-23
      • 2020-03-05
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 2019-08-10
      • 2018-07-09
      相关资源
      最近更新 更多