Итак, я создаю тест, чтобы проверить, вызывает ли мой 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
метод.