【发布时间】:2023-05-07 06:12:01
【问题描述】:
我创建了这个函数,因为对于我的应用程序使用
http.post
发出的所有请求,这就是不同部分处理响应的方式。因此,我想创建一个函数,而不是复制代码。但我无法弄清楚如何对这个函数进行单元测试。
private editAnswerSubject: Subject;
subscribeToReturnedObservable(observable:Observable, subject:Subject) {
observable.subscribe((res) => {
const ev = >(res);
if (ev.type === HttpEventType.Response) {
const isResponseStructureOK: boolean = this.helper.validateServerResponseStructure(ev.body);
if (isResponseStructureOK) {
const response: ServerResponseAPI = ev.body;
subject.next(new Result(response.result, response['additional-info']));
} else {
subject.next(new Result(messages.error, messages.invalidStructureOfResponse));
}
}
},
(error: ServerResponseAPI) => {
const errorMessage: string = this.helper.userFriendlyErrorMessage(error);
subject.next(new Result(messages.error, errorMessage));
},
() => { // observable complete
});
}
editAnswer(answer: Answer): any {
const observable = this.bs.editAnswer(answer)
this.subscribeToReturnedObservable(observable,this.editAnswerSubject);
}
到目前为止我写的测试是
describe('subscribeToReturnedObservable tests:', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [QuestionManagementService, HelperService, WebToBackendInterfaceService, AuthService, HttpClient, HttpHandler]
});
});
fit('should call send next value for the subject is the response from the server is ok', () => {
const questionService:QuestionManagementService = TestBed.get(QuestionManagementService);
const body = {"result":"success", "additional-info":"some additional info"};
const receivedHttpEvent = new HttpResponse({body:body});
let observable = new Observable();
spyOn(observable,'subscribe').and.returnValue(receivedHttpEvent);
spyOn(questionService['editQuestionSubject'],'next');
questionService.subscribeToReturnedObservable(observable,questionService['editQuestionSubject']);
observable.subscribe();
expect(questionService['editQuestionSubject'].next).toHaveBeenCalled();
});
});
但它得到错误
Expected spy next to have been called.
-
spyOn(observable,'subscribe').and.returnValue(receivedHttpEvent);
将不起作用,因为subscribe
返回订阅并且receivedHttpEvent
应该由observable
发出。 -
谢谢马丁。我想我的疑问是如何让
observable.subscribe((res)=>...
与receivedHttpEvent.
一起运行。我无法弄清楚。 -
看看这个 github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/… 或者如果你能做一个stackblitz演示我可以看看
-
谢谢马丁。我尝试了另一种方法。感谢您向我介绍 Marbel 测试。我不知道。我还发现这篇文章很有用 - medium.com/angular-in-depth/…
-
Martin =-我想尝试使用 marbel 测试来模拟错误场景,但我被卡住了。请看一下 stackoverflow.com/questions/61337404/…