-1

When i'm trying to test it's showing the error....

NullInjectorError: R3InjectorError(DynamicTestModule)[AlertService -> InjectionToken config -> InjectionToken config]: 
  NullInjectorError: No provider for InjectionToken config!

This is my component...

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




}




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

sadiquekp
  • 67
  • 1
  • 15

1 Answers1

1

You have an alertService in your constructor, for you tests you'll have to provide this service:

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

This TestAlertService should provide mockdata for all needed requests.

-- Edit after comment: --

Your TestAlertService should have danger as a function as it is used as a function in the original code. Eventual returnvalues have to be of the correct type (boolean, string,...)

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

If this doesn't suffice, please add a piece of your TestAlertService code so I can help more specific

Lotte Lemmens
  • 551
  • 5
  • 11