Angular 8 测试 ngOnInit 没有调用服务方法

问题描述 投票:0回答:1

所以我正在创建一个测试来检查我的 agreementComponent 办法 ngOnInit 正在呼叫 operatorService.getOperators().

到目前为止,我已经设法创造了这样的东西。

import {AgreementComponent} from './agreement.component';
import {async, TestBed} from '@angular/core/testing';
import {ActivatedRoute, convertToParamMap} from '@angular/router';
import {RouterTestingModule} from '@angular/router/testing';
import {OperatorService} from '../../../services/operator.service';
import {of} from 'rxjs/internal/observable/of';
import {FormsModule} from '@angular/forms';
import {AgreementService} from '../../../services/agreement.service';
import {AccountService} from '../../../services/account.service';
import {SessionService} from '../../../services/session.service';
import {SettingsService} from '../../../services/settings.service';

export class MockOperatorService {
  getOperators() {
    return of({data: 'someVal'});
  }
}

export class MockAgreementService {
  getAgreementdraft() {
    return of({data: 'someVal'});
  }
}

export class MockAccountService {
  getFeeAccounts() {
    return of({data: 'someVal'});
  }
}

export class MockSessionService {
  getSession() {
    return of({data: 'someVal'});
  }
}

export class MockSettingsService {
  getSettings() {
    return of({data: 'someVal'});
  }
}

describe('agreementComponent', () => {
  let component;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [AgreementComponent],
      imports: [
        RouterTestingModule,
        FormsModule
      ],
      providers: [
        {
          provide: ActivatedRoute, useValue:
            {
              snapshot: {
                paramMap: convertToParamMap({agreementId: '0'})
              }
            }
        },
        {provide: OperatorService, useClass: MockOperatorService},
        {provide: AgreementService, useClass: MockAgreementService},
        {provide: AccountService, useClass: MockAccountService},
        {provide: SessionService, useClass: MockSessionService},
        {provide: SettingsService, useClass: MockSettingsService},
      ]
    });

  }));

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


  it('should call operators service', () => {
    spyOn(MockOperatorService.prototype, 'getOperators').and.callThrough();

    component.ngOnInit();

    expect(MockOperatorService.prototype.getOperators).toHaveBeenCalled();
  });
});

但我得到的是 Expected spy getOperators to have been called. 我不知道我是否创造了 spyOn 并称为 ngOnInit 方法。

javascript angular typescript karma-jasmine
1个回答
0
投票

试试这个。

在第一个beforeEach块之前声明一个变量为

let operatorService: OperatorService;

在beforeEach块中添加这行 operatorService = TestBed.get(OperatorService); 在底部。

并更新您的测试用例,如

it('should call operators service', () => {
    spyOn(operatorService, 'getOperators');

    component.ngOnInit();

    expect(operatorService.getOperators).toHaveBeenCalled();
  });
© www.soinside.com 2019 - 2024. All rights reserved.