Angular 有人能帮我用这段代码获得100%的代码覆盖率吗?简单地检查函数是否已被调用也可以

Angular 有人能帮我用这段代码获得100%的代码覆盖率吗?简单地检查函数是否已被调用也可以,angular,typescript,testing,jasmine,karma-runner,Angular,Typescript,Testing,Jasmine,Karma Runner,我不知道如何访问剩余的代码行。我只需要得到一个完整的代码覆盖。检查函数是否已被调用(toHaveBeenCalled())也可以 粘贴在TypeScript文件和我的等级库文件中。我目前的代码覆盖范围如下: 总数:2次失败,3次成功 ============================覆盖范围摘要================== 报表:30.34%(27/89) 分行:0%(0/22) 功能:26.92%(7/26) 行数:29.07%(25/86) ==================

我不知道如何访问剩余的代码行。我只需要得到一个完整的代码覆盖。检查函数是否已被调用(toHaveBeenCalled())也可以

粘贴在TypeScript文件和我的等级库文件中。我目前的代码覆盖范围如下:

总数:2次失败,3次成功

============================覆盖范围摘要==================

报表:30.34%(27/89)

分行:0%(0/22)

功能:26.92%(7/26)

行数:29.07%(25/86)

=====================================================

更新通知.component.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import { NotificationService } from '../notification.service';
import { ActivatedRoute, Router } from '@angular/router';
import { ToastrService } from 'ngx-toastr';

@Component({
  selector: 'app-update-notification',
  templateUrl: './update-notification.component.html',
  styleUrls: ['./update-notification.component.scss']
})
export class UpdateNotificationComponent implements OnInit {
  notificationId: any;
  notificationDetails: any;
  parentNotifications: any[];
  editorInstance = ClassicEditor;
  ckEditorConfig = {
    placeholder: 'Add notification description'
  }
  editNotificationGroup = new FormGroup({
    title: new FormControl('', Validators.required),
    parentNotification: new FormControl(),
    description: new FormControl()
  })
  constructor(
    private activeRoute: ActivatedRoute,
    public notificationService: NotificationService,
    private router: Router,
    private toastr: ToastrService
  ) { }

  ngOnInit() {
    this.getParentNotificationsForDropdown();
    this.notificationId = this.activeRoute.snapshot.params.id;
    this.notificationService.getNotificationDetailsById(this.notificationId).
      subscribe((res: any) => {
        this.notificationDetails = res[0];
        this.editNotificationGroup.get('title').setValue(this.notificationDetails.notification_title);
        this.editNotificationGroup.get('description').setValue(this.notificationDetails.notification_message);
        this.editNotificationGroup.get('parentNotification').setValue(this.notificationDetails.linked_notification_id);
      }, error => {
        console.log(error);
        this.editNotificationGroup.get('title').setValue('title');
        this.editNotificationGroup.get('description').setValue('<strong>desc</strong> of test');
        this.editNotificationGroup.get('parentNotification').setValue('null');
      })
  }
  getParentNotificationsForDropdown() {
    this.notificationService.getTradeNotifications().
      subscribe((res: any) => {
        this.parentNotifications = res;
      }, error => {
        console.log('get parent notifications failed', error);
      })
  }

  submitEditNotification() {
    this.notificationService.updatedNotification(this.editNotificationGroup.value, this.notificationId).
      subscribe((res: any) => {
        console.log(res);
        this.toastr.success('Notification updated successfully', 'Notification Blast');
        this.router.navigate(['notifications/list']);
      }, error => {
        console.log(error);
      })
  }

  // if 400 then show error on popup, or else something went wrong or redirect on page

}
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { NotificationService } from '../notification.service';
import { ActivatedRoute } from '@angular/router';
import { Router } from '@angular/router';
import { ToastrService } from 'ngx-toastr';
import { ClassicEditor } from '@ckeditor/ckeditor5-build-classic';
import { UpdateNotificationComponent } from './update-notification.component';

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

  beforeEach(() => {
    const notificationServiceStub = () => ({
      getNotificationDetailsById: notificationId => ({ subscribe: f => f({}) }),
      getTradeNotifications: () => ({ subscribe: f => f({}) }),
      updatedNotification: (value, notificationId) => ({
        subscribe: f => f({})
      })
    });
    const activatedRouteStub = () => ({ snapshot: { params: { id: {} } } });
    const routerStub = () => ({ navigate: array => ({}) });
    const toastrServiceStub = () => ({ success: (string, string1) => ({}) });
    TestBed.configureTestingModule({
      schemas: [NO_ERRORS_SCHEMA],
      declarations: [UpdateNotificationComponent],
      providers: [
        { provide: NotificationService, useFactory: notificationServiceStub },
        { provide: ActivatedRoute, useFactory: activatedRouteStub },
        { provide: Router, useFactory: routerStub },
        { provide: ToastrService, useFactory: toastrServiceStub }
      ]
    });
    fixture = TestBed.createComponent(UpdateNotificationComponent);
    component = fixture.componentInstance;
  });

  it('can load instance', () => {
    expect(component).toBeTruthy();
  });

  it(`editorInstance has default value`, () => {
    expect(component.editorInstance).toEqual(ClassicEditor);
  });

  describe('ngOnInit', () => {
    it('makes expected calls', () => {
      const notificationServiceStub: NotificationService = fixture.debugElement.injector.get(
        NotificationService
      );
      spyOn(component, 'getParentNotificationsForDropdown').and.callThrough();
      spyOn(
        notificationServiceStub,
        'getNotificationDetailsById'
      ).and.callThrough();
      component.ngOnInit();
      expect(component.getParentNotificationsForDropdown).toHaveBeenCalled();
      expect(
        notificationServiceStub.getNotificationDetailsById
      ).toHaveBeenCalled();
    });
  });

  describe('getParentNotificationsForDropdown', () => {
    it('makes expected calls', () => {
      const notificationServiceStub: NotificationService = fixture.debugElement.injector.get(
        NotificationService
      );
      spyOn(notificationServiceStub, 'getTradeNotifications').and.callThrough();
      component.getParentNotificationsForDropdown();
      expect(notificationServiceStub.getTradeNotifications).toHaveBeenCalled();
    });
  });

  describe('submitEditNotification', () => {
    it('makes expected calls', () => {
      const notificationServiceStub: NotificationService = fixture.debugElement.injector.get(
        NotificationService
      );
      const routerStub: Router = fixture.debugElement.injector.get(Router);
      const toastrServiceStub: ToastrService = fixture.debugElement.injector.get(
        ToastrService
      );
      spyOn(notificationServiceStub, 'updatedNotification').and.callThrough();
      spyOn(routerStub, 'navigate').and.callThrough();
      spyOn(toastrServiceStub, 'success').and.callThrough();
      component.submitEditNotification();
      expect(notificationServiceStub.updatedNotification).toHaveBeenCalled();
      expect(routerStub.navigate).toHaveBeenCalled();
      expect(toastrServiceStub.success).toHaveBeenCalled();
    });
  });
});
从'@angular/core'导入{Component,OnInit};
从'@angular/forms'导入{FormGroup,FormControl,Validators};
从“@ckeditor/ckeditor5 build classic”导入*作为ClassicEditor;
从“../notification.service”导入{NotificationService};
从'@angular/Router'导入{ActivatedRoute,Router};
从“ngx-toastr”导入{ToastrService};
@组成部分({
选择器:“应用程序更新通知”,
templateUrl:'./update notification.component.html',
样式URL:['./更新通知.component.scss']
})
导出类UpdateNotificationComponent实现OnInit{
通知ID:任何;
通知详情:任何;
家长通知:任何[];
editorInstance=ClassicEditor;
ckEditorConfig={
占位符:“添加通知说明”
}
editNotificationGroup=新表单组({
标题:新FormControl(“”,需要验证器),
parentNotification:new FormControl(),
描述:新表单控件()
})
建造师(
专用activeRoute:ActivatedRoute,
公共通知服务:通知服务,
专用路由器:路由器,
私人toastr:ToastrService
) { }
恩戈尼尼特(){
this.getParentNotificationsForDropdown();
this.notificationId=this.activeRoute.snapshot.params.id;
this.notificationService.getNotificationDetailsById(this.notificationId)。
订阅((res:any)=>{
this.notificationDetails=res[0];
this.editNotificationGroup.get('title').setValue(this.notificationDetails.notification_title);
this.editNotificationGroup.get('description').setValue(this.notificationDetails.notification_消息);
this.editNotificationGroup.get('parentNotification').setValue(this.notificationDetails.linked_notification_id);
},错误=>{
console.log(错误);
this.editNotificationGroup.get('title').setValue('title');
this.editNotificationGroup.get('description').setValue('test的'desc);
this.editNotificationGroup.get('parentNotification').setValue('null');
})
}
getParentNotificationsForDropdown(){
此.notificationService.getTradeNotifications()已被删除。
订阅((res:any)=>{
this.parentNotifications=res;
},错误=>{
console.log('get parent notification failed',error);
})
}
提交的通知(){
this.notificationService.updatedNotification(this.editNotificationGroup.value,this.notificationId)。
订阅((res:any)=>{
控制台日志(res);
this.toastr.success('Notification updated successfully'、'Notification Blast');
this.router.navigate(['notifications/list']);
},错误=>{
console.log(错误);
})
}
//若400,则在弹出窗口上显示错误,或者页面上出现错误或重定向
}
我的测试用例

更新notificatoin.component.spec.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import { NotificationService } from '../notification.service';
import { ActivatedRoute, Router } from '@angular/router';
import { ToastrService } from 'ngx-toastr';

@Component({
  selector: 'app-update-notification',
  templateUrl: './update-notification.component.html',
  styleUrls: ['./update-notification.component.scss']
})
export class UpdateNotificationComponent implements OnInit {
  notificationId: any;
  notificationDetails: any;
  parentNotifications: any[];
  editorInstance = ClassicEditor;
  ckEditorConfig = {
    placeholder: 'Add notification description'
  }
  editNotificationGroup = new FormGroup({
    title: new FormControl('', Validators.required),
    parentNotification: new FormControl(),
    description: new FormControl()
  })
  constructor(
    private activeRoute: ActivatedRoute,
    public notificationService: NotificationService,
    private router: Router,
    private toastr: ToastrService
  ) { }

  ngOnInit() {
    this.getParentNotificationsForDropdown();
    this.notificationId = this.activeRoute.snapshot.params.id;
    this.notificationService.getNotificationDetailsById(this.notificationId).
      subscribe((res: any) => {
        this.notificationDetails = res[0];
        this.editNotificationGroup.get('title').setValue(this.notificationDetails.notification_title);
        this.editNotificationGroup.get('description').setValue(this.notificationDetails.notification_message);
        this.editNotificationGroup.get('parentNotification').setValue(this.notificationDetails.linked_notification_id);
      }, error => {
        console.log(error);
        this.editNotificationGroup.get('title').setValue('title');
        this.editNotificationGroup.get('description').setValue('<strong>desc</strong> of test');
        this.editNotificationGroup.get('parentNotification').setValue('null');
      })
  }
  getParentNotificationsForDropdown() {
    this.notificationService.getTradeNotifications().
      subscribe((res: any) => {
        this.parentNotifications = res;
      }, error => {
        console.log('get parent notifications failed', error);
      })
  }

  submitEditNotification() {
    this.notificationService.updatedNotification(this.editNotificationGroup.value, this.notificationId).
      subscribe((res: any) => {
        console.log(res);
        this.toastr.success('Notification updated successfully', 'Notification Blast');
        this.router.navigate(['notifications/list']);
      }, error => {
        console.log(error);
      })
  }

  // if 400 then show error on popup, or else something went wrong or redirect on page

}
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { NotificationService } from '../notification.service';
import { ActivatedRoute } from '@angular/router';
import { Router } from '@angular/router';
import { ToastrService } from 'ngx-toastr';
import { ClassicEditor } from '@ckeditor/ckeditor5-build-classic';
import { UpdateNotificationComponent } from './update-notification.component';

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

  beforeEach(() => {
    const notificationServiceStub = () => ({
      getNotificationDetailsById: notificationId => ({ subscribe: f => f({}) }),
      getTradeNotifications: () => ({ subscribe: f => f({}) }),
      updatedNotification: (value, notificationId) => ({
        subscribe: f => f({})
      })
    });
    const activatedRouteStub = () => ({ snapshot: { params: { id: {} } } });
    const routerStub = () => ({ navigate: array => ({}) });
    const toastrServiceStub = () => ({ success: (string, string1) => ({}) });
    TestBed.configureTestingModule({
      schemas: [NO_ERRORS_SCHEMA],
      declarations: [UpdateNotificationComponent],
      providers: [
        { provide: NotificationService, useFactory: notificationServiceStub },
        { provide: ActivatedRoute, useFactory: activatedRouteStub },
        { provide: Router, useFactory: routerStub },
        { provide: ToastrService, useFactory: toastrServiceStub }
      ]
    });
    fixture = TestBed.createComponent(UpdateNotificationComponent);
    component = fixture.componentInstance;
  });

  it('can load instance', () => {
    expect(component).toBeTruthy();
  });

  it(`editorInstance has default value`, () => {
    expect(component.editorInstance).toEqual(ClassicEditor);
  });

  describe('ngOnInit', () => {
    it('makes expected calls', () => {
      const notificationServiceStub: NotificationService = fixture.debugElement.injector.get(
        NotificationService
      );
      spyOn(component, 'getParentNotificationsForDropdown').and.callThrough();
      spyOn(
        notificationServiceStub,
        'getNotificationDetailsById'
      ).and.callThrough();
      component.ngOnInit();
      expect(component.getParentNotificationsForDropdown).toHaveBeenCalled();
      expect(
        notificationServiceStub.getNotificationDetailsById
      ).toHaveBeenCalled();
    });
  });

  describe('getParentNotificationsForDropdown', () => {
    it('makes expected calls', () => {
      const notificationServiceStub: NotificationService = fixture.debugElement.injector.get(
        NotificationService
      );
      spyOn(notificationServiceStub, 'getTradeNotifications').and.callThrough();
      component.getParentNotificationsForDropdown();
      expect(notificationServiceStub.getTradeNotifications).toHaveBeenCalled();
    });
  });

  describe('submitEditNotification', () => {
    it('makes expected calls', () => {
      const notificationServiceStub: NotificationService = fixture.debugElement.injector.get(
        NotificationService
      );
      const routerStub: Router = fixture.debugElement.injector.get(Router);
      const toastrServiceStub: ToastrService = fixture.debugElement.injector.get(
        ToastrService
      );
      spyOn(notificationServiceStub, 'updatedNotification').and.callThrough();
      spyOn(routerStub, 'navigate').and.callThrough();
      spyOn(toastrServiceStub, 'success').and.callThrough();
      component.submitEditNotification();
      expect(notificationServiceStub.updatedNotification).toHaveBeenCalled();
      expect(routerStub.navigate).toHaveBeenCalled();
      expect(toastrServiceStub.success).toHaveBeenCalled();
    });
  });
});
从'@angular/core/testing'导入{ComponentFixture,TestBed};
从'@angular/core'导入{NO_ERRORS_SCHEMA};
从“../notification.service”导入{NotificationService};
从'@angular/router'导入{ActivatedRoute};
从'@angular/Router'导入{Router};
从“ngx-toastr”导入{ToastrService};
从“@ckeditor/ckeditor5 build classic”导入{ClassicEditor};
从“./update notification.component”导入{UpdateNotificationComponent};
描述('UpdateNotificationComponent',()=>{
let组件:UpdateNotificationComponent;
let夹具:组件夹具;
在每个之前(()=>{
const notificationServiceStub=()=>({
getNotificationDetailsById:notificationId=>({subscribe:f=>f({})}),
getTradeNotifications:()=>({subscribe:f=>f({})}),
updatedNotification:(值,notificationId)=>({
订阅:f=>f({})
})
});
const activatedRouteStub=()=>({snapshot:{params:{id:{}});
constrouterstub=()=>({navigate:array=>({})});
const toastrServiceStub=()=>({success:(string,string1)=>({}});
TestBed.configureTestingModule({
模式:[无错误模式],
声明:[UpdateNotificationComponent],
供应商:[
{提供:NotificationService,使用工厂:notificationServiceStub},
{提供:ActivatedRoute,使用工厂:activatedRouteStub},
{提供:路由器,使用工厂:routerStub},
{提供:ToastrService,使用工厂:toastrServiceStub}
]
});
fixture=TestBed.createComponent(UpdateNotificationComponent);
组件=fixture.componentInstance;
});
它('可以加载实例',()=>{
expect(component.toBeTruthy();
});
它(`editorInstance有默认值`,()=>{
expect(component.editorInstance).toEqual(ClassicEditor);
});
描述('ngOnInit',()=>{
它('发出预期呼叫',()=>{
const notificationServiceStub:NotificationService=fixture.debugElement.injector.get(
通知服务
);
spyOn(组件“getParentNotificationsForDropdown”)和.callThrough();
间谍(
通知服务存根,
“GetNotificationDetailsByd”
).和.callThrough();
组件。ngOnInit();
expect(component.getParentNotificationsForDropdown).toHaveBeenCalled();