首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Karma测试Angular - this.alertService.danger不是一个函数

Karma测试Angular - this.alertService.danger不是一个函数
EN

Stack Overflow用户
提问于 2020-07-27 16:01:28
回答 1查看 136关注 0票数 0

当我尝试使用karma进行测试时,问题显示..

TypeError: this.alertService.danger is not a function

这是我的组件,

代码语言:javascript
复制
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,

代码语言:javascript
复制
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();
  });
});

Thanks..

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-27 16:11:40

它在你的TestAlertService中应该有函数危险。尝试仅在规范中创建模拟类。

代码语言:javascript
复制
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();
});
});

这对你来说应该是可行的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63111060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档