Я относительно новичок в модульном тестировании Angular. Я использую Жасмин, и я считаю, что API расплывчато. Я пытаюсь написать хорошие тесты для моего компонента, но я застрял на том, как тестировать компонент, который подписывается на данные из службы.
// component
ngOnInit() {
this.store.dispatch(this.loadDatagridDataAction({data: this.authorizations}));
this.authorizationService.initAuthorizations();
this.authorizationService.authorizations.subscribe(authorizations => {
this.realAuthorizations = authorizations;
this.realAuthorizations.results.forEach(authorization => {
const formatAuthorization = {};
if (authorization.number) {
formatAuthorization['number'] = authorization.number;
}
if (authorization.status) {
formatAuthorization['status'] = authorization.status;
}
if (authorization.requesting_provider) {
const requestingProvider = flattenObject(authorization.requesting_provider);
formatAuthorization['requesting_provider'] = requestingProvider['name'];
}
if (authorization.servicing_provider) {
const servicingProvider = flattenObject(authorization.servicing_provider);
formatAuthorization['servicing_provider'] = servicingProvider['name'];
}
if (authorization.termination_date) {
formatAuthorization['termination_date'] = authorization.termination_date;
}
this.formattedAuthorizations.push(formatAuthorization);
});
});
}
setActiveTab(tab) {
this.store.dispatch(this.setActiveTabAction({tab}));
}
setActiveRow(row) {
console.log(row);
}
setCurrentPage(page) {
this.store.dispatch(this.setPaginatorCurrentPageAction({page}));
}
goBack(route) {
console.log(route);
}
}
// service
export class AuthorizationService {
private _authorizations = new Subject<any>();
constructor(
private http: HttpClient,
) { }
public get authorizations() {
return this._authorizations.asObservable();
}
public getAuthorizations() {
const auth = '/api/enrollment/tocs/';
return this.http.get(auth);
}
public initAuthorizations() {
this.getAuthorizations().subscribe(data => {
this._authorizations.next(data);
}, error => {
console.log('Failed to get data', error);
});
}
}
// unit test
describe('AuthorizationsReferralsComponent', () => {
let component: AuthorizationsReferralsComponent;
let fixture: ComponentFixture<AuthorizationsReferralsComponent>;
let storeMock;
let configurationServiceMock;
let authorizationMockService;
beforeEach(async(() => {
storeMock = {
select: jasmine.createSpy('select'),
dispatch: jasmine.createSpy('dispatch'),
};
authorizationMockService = jasmine.createSpyObj(['getAuthorizations', 'initAuthorizations', 'authorizations']);
configurationServiceMock = {
getTemplateConfiguration: () => {}
};
TestBed.configureTestingModule({
declarations: [ AuthorizationsReferralsComponent ],
imports: [SidebarTableViewModule, HttpClientTestingModule],
providers: [
{
provide: Store,
useValue: storeMock
},
{
provide: ConfigurationService,
useValue: configurationServiceMock,
},
{
provide: AuthorizationService,
useValue: authorizationMockService
},
]
})
.compileComponents();
}));
beforeEach(() => {
HEROES = [
{id: 1, name: 'SpiderDude', strength: 8},
{id: 2, name: 'Wonderful Woman', strength: 24},
{id: 3, name: 'SuperDude', strength: 55}
];
fixture = TestBed.createComponent(AuthorizationsReferralsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
authorizationMockService.initAuthorizations.and.returnValue(of(HEROES));
fixture.detectChanges();
expect(fixture.componentInstance.realAuthorizations.length).toBe(3);
});
});
Я получаю следующая ошибка TypeError: this.authorizationService.authorizations.subscribe is not a function
Похоже, что она думает, что моя get authorizations()
функция не является функцией. Как я могу соответствующим образом смоделировать свой сервис, чтобы проверить утверждения внутри моей подписки?
this.authorizationService.authorizations.subscribe(