angular - Angular 6 中的服务测试 - mockBackend
问题描述
我正在尝试在我的服务中测试一个返回 observable (BehaviorSubject) 的函数。
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject, Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class TopicsService {
private topics = [];
private obs = new BehaviorSubject<any>(this.topics);
constructor(private http: HttpClient) {}
getTopics(): BehaviorSubject<any> {
if (!this.topics.length) {
this.http.get('http://localhost:8089/topics')
.subscribe(
(topicsData: any) => {
this.topics = topicsData;
this.obs.next(this.topics);
}
);
}
return this.obs;
}
然后我创建了一个我希望在 Http 假调用中接收到的 mockResponse 对象。然后我调用 getTopics() 函数并订阅 Observable,我希望得到的实际上是响应的主体:
import { TestBed, inject } from '@angular/core/testing';
import { HttpClient, HttpHandler } from '@angular/common/http';
import { ConnectionBackend, ResponseOptions } from '@angular/http';
import { MockBackend } from '@angular/http/testing';
import { TopicsService } from './topics.service';
describe('TopicsService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
TopicsService, HttpClient, HttpHandler,
{ provide: ConnectionBackend, useClass: MockBackend }
]
});
});
describe('#getTopics()', () => {
it('should be created', inject([TopicsService], (service: TopicsService) => {
expect(service).toBeTruthy();
}));
it('should return a BehaviourSubject<any<Topic>>',
inject([TopicsService, ConnectionBackend], (service: TopicsService, mockBackend) => {
const mockResponse = {
name: 'Chess',
decription: 'Best chess courses'
};
mockBackend.connections.subscribe((connection) => {
connection.mockRespond(new Response(new ResponseOptions({
body: JSON.stringify(mockResponse)
})));
});
service.getTopics().subscribe((topicData) => {
expect(topicData.length).toBe(1);
expect(topicData.name).toEqual('Chess');
expect(topicData.description).toEqual('Best chess courses');
});
}));
});
});
我显然做错了什么,但我似乎无法理解。提前非常感谢!
编辑:对不起,我忘了添加测试输出,我的错:
TopicsService #getTopics() should return a BehaviourSubject<any<Topic>> FAILED
Expected 0 to be 1.
at SafeSubscriber._next src/services/topics.service.spec.ts:38:34)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub node_modules/rxjs/_esm5/internal/Subscriber.js:195:1)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next node_modules/rxjs/_esm5/internal/Subscriber.js:133:1)
at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next node_modules/rxjs/_esm5/internal/Subscriber.js:77:1)
Expected undefined to equal 'Chess'.
at SafeSubscriber._next src/services/topics.service.spec.ts:39:32)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub node_modules/rxjs/_esm5/internal/Subscriber.js:195:1)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next node_modules/rxjs/_esm5/internal/Subscriber.js:133:1)
at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next node_modules/rxjs/_esm5/internal/Subscriber.js:77:1)
Expected undefined to equal 'Best chess courses'.
at SafeSubscriber._next src/services/topics.service.spec.ts:40:39)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub node_modules/rxjs/_esm5/internal/Subscriber.js:195:1)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next node_modules/rxjs/_esm5/internal/Subscriber.js:133:1)
解决方案
推荐阅读
- c# - 在 C# 中为 TTS 调用 SAPI,但它使用隐藏菜单而不是 windows 默认值
- android - 屏幕关闭时前台服务变慢
- ruby-on-rails - 有效证书上的 Rails SSL 证书错误
- java - Spring Boot org.springframework.transaction.UnexpectedRollbackException:事务静默回滚,因为它已被标记为仅回滚
- json - 从一个请求中提取一个属性值,并在运行独立的wiremock服务器时使用不同的请求
- python - 如何在我的 python 代码中启用 Intel Extension for Pytorch(IPEX)?
- python - BananaPi m2 零 GPIO 调用总是导致打印
- html - 如何将一行按钮显示为网格
- ios - iOS:使用 UINavigationBarAppearance 时指定状态栏文本颜色现在 barstyle 被忽略
- javascript - WebGL 流体渐变背景与流体 Canvas.js