Angular 业力测试角度-此.alertService.danger不是功能

Angular 业力测试角度-此.alertService.danger不是功能,angular,typescript,karma-jasmine,karma-coverage,Angular,Typescript,Karma Jasmine,Karma Coverage,当我尝试使用业力测试时,问题显示 TypeError:this.alertService.danger不是一个函数 这是我的组件 import { Component, OnInit, ViewChild, ElementRef, AfterViewChecked } from '@angular/core'; import { Router } from '@angular/router'; import { ApiService } from '../services/api.servic

当我尝试使用业力测试时,问题显示

TypeError:this.alertService.danger不是一个函数

这是我的组件

import { Component, OnInit, ViewChild, ElementRef, AfterViewChecked } from '@angular/core';
import { Router  } from '@angular/router';
import { ApiService } from '../services/api.service';
import { NgxSpinnerService } from "ngx-spinner";
import { AlertService } from 'ngx-alerts';

@Component({
  selector: 'app-review',
  templateUrl: './review.component.html',
  styleUrls: ['./review.component.scss']
})
export class ReviewComponent implements OnInit  {
  @ViewChild('scrollMe', { read: ElementRef }) public scroll: ElementRef;
  @ViewChild('scrollMe', { read: ElementRef }) public scrollbottom: ElementRef;

  cartDetails: any;
  cartId:any;
  menuItems:any;
  expectedDeliveryTime:any
  noOfNulls: any;

  constructor(
    public router: Router,
    public apiService: ApiService,
    private alertService: AlertService,
    private spinner: NgxSpinnerService) { 
    this.spinner.show();
    this.cartId = localStorage.getItem('cartId');
    
    if(!this.cartId){
      
      this.noOfNulls = localStorage.getItem('noOfNulls');
    
      if(!this.noOfNulls){
        localStorage.setItem('noOfNulls','1')
        this.alertService.danger("Please add at least one product to the cart.");
        this.changeSomething();
      } else if(this.noOfNulls==1) {
        localStorage.setItem('noOfNulls','2');
        this.alertService.danger("Please add at least one product to the cart.");
        this.changeSomething();
      }
      else{
        localStorage.setItem('noOfNulls','')
        this.router.navigate(['home']);
      }


    } 

  }

  ngOnInit(): void {
    this.apiService.getCartDetais(this.cartId).subscribe((res)=>{
      this.spinner.hide();
      this.cartDetails = res.body.cart;
      this.menuItems = res.body.cart.menuItems;

      this.expectedDeliveryTime = res.body.expectedDeliveryTime;
      localStorage.setItem('expectedDeliveryTime',this.expectedDeliveryTime)

    });
    

  }

  confirm() {
    this.router.navigate(['subscription']);
  }

  goBack() {
    localStorage.setItem('val','0');
    this.router.navigate(['product']);
  }

  cancel() {
    this.router.navigate(['home'])
  }
  
  changeSomething(){
    localStorage.setItem('val','0');
    this.router.navigate(['product']);
    
  }


  

}

spect.ts

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { ReviewComponent } from './review.component';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClient, HttpClientModule, HttpHandler } from '@angular/common/http';
import { FormBuilder } from '@angular/forms';
import { AlertService } from 'ngx-alerts';
import { TestAlertService } from '../services/test-alert.service';

describe('ReviewComponent', () => {
  let component: ReviewComponent;
  let fixture: ComponentFixture<ReviewComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ ReviewComponent ],
      imports: [RouterTestingModule,HttpClientModule],
      providers: [
        HttpClient,
        FormBuilder,
        HttpHandler,
        { 
          provide: AlertService, 
          useClass: TestAlertService 
        }
      ]
    })
    .compileComponents();
  }));

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

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

从'@angular/core/testing'导入{async,ComponentFixture,TestBed};
从“/review.component”导入{ReviewComponent};
从“@angular/router/testing”导入{RouterTestingModule};
从'@angular/common/http'导入{HttpClient,HttpClientModule,HttpHandler};
从'@angular/forms'导入{FormBuilder};
从“ngx警报”导入{AlertService};
从“../services/test alert.service”导入{TestAlertService};
描述('ReviewComponent',()=>{
let组件:ReviewComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[审阅组件],
导入:[RouterTestingModule,HttpClientModule],
供应商:[
HttpClient,
造模工,
HttpHandler,
{ 
提供:警报服务,
useClass:TestAlertService
}
]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(ReviewComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
});
});

谢谢,在您的测试服务中,它应该有功能危险。尝试仅在规范中创建模拟类

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { ReviewComponent } from './review.component';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClient, HttpClientModule, HttpHandler } from '@angular/common/http';
import { FormBuilder } from '@angular/forms';
import { AlertService } from 'ngx-alerts';

export class TestAlertService {
  danger() {
    console.log("Danger Alert")
  }
}

describe('ReviewComponent', () => {
let component: ReviewComponent;
let fixture: ComponentFixture<ReviewComponent>;

beforeEach(async(() => {
    TestBed.configureTestingModule({
    declarations: [ ReviewComponent ],
    imports: [RouterTestingModule,HttpClientModule],
    providers: [
        HttpClient,
        FormBuilder,
        HttpHandler,
        { 
        provide: AlertService, 
        useClass: TestAlertService 
        }
    ]
    })
    .compileComponents();
}));

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

it('should create', () => {
    expect(component).toBeTruthy();
});
});
从'@angular/core/testing'导入{async,ComponentFixture,TestBed};
从“/review.component”导入{ReviewComponent};
从“@angular/router/testing”导入{RouterTestingModule};
从'@angular/common/http'导入{HttpClient,HttpClientModule,HttpHandler};
从'@angular/forms'导入{FormBuilder};
从“ngx警报”导入{AlertService};
导出类TestAlertService{
危险(){
控制台日志(“危险警报”)
}
}
描述('ReviewComponent',()=>{
let组件:ReviewComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[审阅组件],
导入:[RouterTestingModule,HttpClientModule],
供应商:[
HttpClient,
造模工,
HttpHandler,
{ 
提供:警报服务,
useClass:TestAlertService
}
]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(ReviewComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
});
});

它应该适合您。

您的TestAlertService是否具有危险功能?如果不是,创建一个函数。一个模拟函数,对吗?