我有一个数据服务,它从服务器获取数据并发出多个请求,然后返回一个可观察的数组。我想测试数据。
我尝试做的是在我发送的包含两个可观察对象的模拟响应中,我不知道这是否是测试数据的正确方法。
但是测试失败了,尤其是异步测试块中的最后三个测试
重要提示:我想测试一下,当将 charId 设置为 falsy 并将 ComicsId 设置为 falsy 时,调用 tthe 方法,订阅它返回的 observable,在您模拟 http 之后,您会返回一个包含两个预期响应的数组。如果 charId 为真,则与 4 预期响应相同。当comicsId 为真时,6 个预期响应相同
// 获取数据的服务
getChar(): Observable<any> {
const Observables = [];
Observables.push(this.http.get('https://gateway.marvel.com:443/v1/public/characters?apikey'));
Observables.push(this.http.get('https://gateway.marvel.com:443/v1/public/comics?apikey'));
if (this.charId) {
Observables.push(this.http.get(`${this.urlChar}${this.charId}${this.apiKey}`));
Observables.push(this.http.get(`${this.urlChar}${this.charId}/comics${this.apiKey}`));
}
if (this.comicsId) {
Observables.push(this.http.get(`${this.urlCom}${this.comicsId}${this.apiKey}`));
Observables.push(this.http.get(`${this.urlCom}${this.comicsId}/creators${this.apiKey}`));
}
console.log([Observable, Observable]);
return Observable.forkJoin(Observables);
}
}
// 我的测试
import { async, ComponentFixture, TestBed, getTestBed, inject } from '@angular/core/testing';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { DataService } from './data.service';
import {
BaseRequestOptions, Http, XHRBackend, HttpModule,
Response, ResponseOptions, RequestMethod
} from '@angular/http';
import { Observable } from 'rxjs/Observable';
describe('DataService', () => {
let mockBackend: MockBackend;
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
DataService,
MockBackend,
BaseRequestOptions,
{
provide: Http,
deps: [MockBackend, BaseRequestOptions],
useFactory:
(backend: XHRBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
}
}
],
imports: [
HttpModule
]
});
mockBackend = getTestBed().get(MockBackend);
}));
it('should get ObservableArr', (done) => {
let dataService: DataService;
getTestBed().compileComponents().then(() => {
mockBackend.connections.subscribe(
(connection: MockConnection) => {
connection.mockRespond(new Response(
new ResponseOptions({
body: [Observable, Observable]
}
)));
});
dataService = getTestBed().get(DataService);
expect(DataService).toBeDefined();
dataService.getChar().subscribe((obsArr: Observable<any>[]) => {
expect(obsArr.length).toBeDefined();
expect(obsArr.length).toEqual(2);
expect(obsArr.length).not.toBe(1);
done();
});
});
});
it('should check the service',
inject([DataService], (service: DataService) => {
expect(service).toBeTruthy();
}));
it('should get ObservableArray async',
async(inject([DataService], (dataService: DataService) => {
mockBackend.connections.subscribe(
(connection: MockConnection) => {
connection.mockRespond(new Response(
new ResponseOptions({
body: [Observable, Observable]
}
)));
});
dataService.getChar().subscribe(
(response) => {
expect(response.length).toBe(2);
expect(response[0]).toBe(Observable); <<<<<<<<<<<<<< Fails
expect(response[1]).toBe(Observable); <<<<<<<<<<<<<< Fails
expect(response).toEqual([Observable, Observable]); <<<<<< Fails
});
})));
});