2
我試圖爲在Typescript/Angular2中編寫的模擬服務設置一些單元測試。 當我嘗試調用從服務的任何方法在我的單元測試,我得到使用observables進行單元測試模擬服務=>訂閱不是函數
TypeError: this._subscribe is not a function
在正常使用該服務工作正常。
我的虛擬服務看起來是這樣的:
import {Observable} from 'rxjs/Rx';
import {Zahlungsverbindung, ZahlungsverbindungArtEnum} from '../../model/zahlungsverbindung.model';
import {Response} from '@angular/http';
import {RestData} from '../RestData';
import {Injectable} from '@angular/core';
@Injectable()
export class DummyDataServiceMock {
private _someData : RestData = this.createDummyData();
public getDummyData(): Observable<RestData> {
return Observable.create(this._someData);
}
private createDummyData(): RestData {
let id = 1;
let usable = true;
let someValue = 'AKTUELL';
let otherValue = 'Dummy Person';
let restData: RestData = {
id: id,
usable: usable,
someValue: someValue,
otherValue: otherValue,
}
return restData;
}
}
這裏是RestData:
export interface RestData {
id?: number;
usable?: boolean;
someValue?: string;
otherValue?: string;
}
而失敗的單元測試:
import {DummyDataServiceMock} from '../DummyData.service.mock.ts';
import {RestData} from '../RestData.ts';
import {TestBed, inject, async} from '@angular/core/testing';
describe('DummyService (Mocked)',() => {
let service;
beforeEach(() => TestBed.configureTestingModule({
providers: [DummyDataServiceMock],
}));
beforeEach(inject([DummyDataServiceMock], s => {
service = s;
}));
it('Service should be defined', async(() => {
expect(service).toBeDefined();
}));
it('Get dummy data from service', async(() => {
let restData: RestData;
service.getDummyData().subscribe(data => {
restData = data
expect(restData.id).toBeDefined();
});
}));
})
我嘗試過很多辦法來設置測試,但他們都沒有工作。 我也在互聯網上搜索很多,但我找不到任何人有同樣的問題。 奇怪的是,該服務被定義,當我做
我得到:
Observable {_isScalar: false, _subscribe: Object}
我希望你們能幫助我了一次。