如何为此代码块编写业力单元测试

时间:2018-08-07 19:13:36

标签: angular karma-jasmine

我想知道如何为下面找到的所有代码编写业力单元测试,我正在使用Angular 6,这是我的save-actions-manager.service.ts文件:

import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable, of } from 'rxjs';
import { HttpHeaders, HttpClient } from '@angular/common/http';
import { EquipmentCardModel } from '../../models/equipment-card-model';
import { EquipmentListManagerService } from '../managers/equipment-list-manager.service';
import { ActionService } from '../data/action.service';

@Injectable({
  providedIn: 'root'
})
export class SaveActionsManagerService {
  equipmentCardModels: EquipmentCardModel[] = [];

  constructor(
    private http: HttpClient,
    private equipmentListManagerService: EquipmentListManagerService,
    private actionService: ActionService
  ) {}

  saveTapAction(list, trackId, direction) {
    const equipmentIds = [];

    for (let i = 0; i < list.length; i++) {
      equipmentIds.push({
        initial: list[i].equipment.equipmentId.equipmentInitial,
        number: list[i].equipment.equipmentId.equipmentNumber
      });
    }
    this.actionService.sendToResequence(direction, equipmentIds, trackId).subscribe(res => {
      this.equipmentListManagerService.loadEquipmentList(trackId, direction);
      this.equipmentListManagerService.togglePreventChecks(true);
    });
  }

这是我为此编写的当前测试,它通过了但未测试整个块,这是我的save-actions-manager.service.spec.ts

import { TestBed, inject } from '@angular/core/testing';
import { MatSnackBarModule } from '@angular/material';
import { SaveActionsManagerService } from './save-actions-manager.service';
import { HttpHandler, HttpClient } from '@angular/common/http';
import { ActionService } from '../data/action.service';
import { initServicesIfNeeded } from '@angular/core/src/view/services';
import { BehaviorSubject, Observable, of } from 'rxjs';


describe('SaveActionsManagerService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [MatSnackBarModule],
      providers: [SaveActionsManagerService, HttpClient, HttpHandler]
    });
  });

  it(
    'should be created',
    inject([SaveActionsManagerService], (service: SaveActionsManagerService) => {
      expect(service).toBeTruthy();
    })
  );

  it(
    'should call saveTapAction()',
    inject([SaveActionsManagerService], (service: SaveActionsManagerService) => {
      const mockList = [
        {
          equipment: {
            equipmentId: {
              equipmentInitial: 'test',
              equipmentNumber: 1234
            }
          },
          trackId: {
            id: 1234,
            stationId: {
              circ7: 'DM004',
              id: 1234
            },
            yardTrackNumber: {
              trackNumber: 8,
              yardNumber: 1
            }
          }
        }
      ];
      service.saveTapAction(mockList, 1234, 'N');
    })
  );

任何帮助将不胜感激,我也是编写业力茉莉花单元测试的超级新手。

0 个答案:

没有答案