在 Jasmine 中调用函数时如何保留它
How to preserve this when calling a function in Jasmine
我正在为服务编写测试。
我正在测试的服务片段是:
getDesign(designId: number): Observable<any> {
const params = new HttpParams().set('designId', String(designId));
return this.http
.get('/get-design', { params: params })
.pipe(retry(1), map((res) => res['data'] || []), catchError(this.handleError));
}
规范文件的片段是:
fit('getDesign should append params and return', () => {
const response = {
'success': true,
'data': [{foo: 'bar'}],
'metadata': {'version': '1.8.25', 'versionHash': '14e456e062583f176d25'},
'total': 0,
'message': []
};
// service.getDesign(42).subscribe((res) => {
// expect(res).toEqual([{foo: 'bar'}]);
// });
//
// const call: TestRequest = httpMock.expectOne('/get-design?designId=42');
// expect(call.request.method).toEqual('GET');
// call.flush(response);
doServiceTest(service.getDesign, response, 42, [{foo: 'bar'}],
'/get-design?designId=42', 'POST');
});
function doServiceTest(myFunction: Function, myResponse, myParam, myExpected, myUrl, myMethod) {
myFunction.bind(this);
myFunction(myParam).subscribe((res) => {
expect(res).toEqual(myExpected);
});
const call: TestRequest = httpMock.expectOne(myUrl);
expect(call.request.method).toEqual(myMethod);
call.flush(myResponse);
}
被注释掉的代码工作得很好,所以这不是没有注入或未引用的问题,这会使我对其他 Stack Overflow 帖子和搜索结果的答案无效 运行穿过。
我更愿意使用另一种方法,因为我有多个函数,它们的区别仅在于 params
和 expected
。
但是,当我尝试这种方式时,出现 TypeError: Cannot read property 'http' of undefined
错误,它告诉我 this
引用不知何故丢失了。
我做错了什么?
我什至试过显式绑定服务和这个,但没有解决问题。
通过反复试验终于弄明白了。
对于可能 运行 跨越此 post 的任何人,解决方案是从显式函数调用更改为隐式函数调用,如下所示。
fit('getDesign should append params and return', () => {
const response = {
'success': true,
'data': [{foo: 'bar'}],
'metadata': {'version': '1.8.25', 'versionHash': '14e456e062583f176d25'},
'total': 0,
'message': []
};
doServiceTest('getDesign', response, 42, [{foo: 'bar'}],
'/get-design?designId=42', 'GET');
});
function doServiceTest(myFunction: string, myResponse, myParam, myExpected, myUrl, myMethod) {
service[myFunction](myParam).subscribe((res) => {
expect(res).toEqual(myExpected);
});
const call: TestRequest = httpMock.expectOne(myUrl);
expect(call.request.method).toEqual(myMethod);
call.flush(myResponse);
}
我正在为服务编写测试。
我正在测试的服务片段是:
getDesign(designId: number): Observable<any> {
const params = new HttpParams().set('designId', String(designId));
return this.http
.get('/get-design', { params: params })
.pipe(retry(1), map((res) => res['data'] || []), catchError(this.handleError));
}
规范文件的片段是:
fit('getDesign should append params and return', () => {
const response = {
'success': true,
'data': [{foo: 'bar'}],
'metadata': {'version': '1.8.25', 'versionHash': '14e456e062583f176d25'},
'total': 0,
'message': []
};
// service.getDesign(42).subscribe((res) => {
// expect(res).toEqual([{foo: 'bar'}]);
// });
//
// const call: TestRequest = httpMock.expectOne('/get-design?designId=42');
// expect(call.request.method).toEqual('GET');
// call.flush(response);
doServiceTest(service.getDesign, response, 42, [{foo: 'bar'}],
'/get-design?designId=42', 'POST');
});
function doServiceTest(myFunction: Function, myResponse, myParam, myExpected, myUrl, myMethod) {
myFunction.bind(this);
myFunction(myParam).subscribe((res) => {
expect(res).toEqual(myExpected);
});
const call: TestRequest = httpMock.expectOne(myUrl);
expect(call.request.method).toEqual(myMethod);
call.flush(myResponse);
}
被注释掉的代码工作得很好,所以这不是没有注入或未引用的问题,这会使我对其他 Stack Overflow 帖子和搜索结果的答案无效 运行穿过。
我更愿意使用另一种方法,因为我有多个函数,它们的区别仅在于 params
和 expected
。
但是,当我尝试这种方式时,出现 TypeError: Cannot read property 'http' of undefined
错误,它告诉我 this
引用不知何故丢失了。
我做错了什么?
我什至试过显式绑定服务和这个,但没有解决问题。
通过反复试验终于弄明白了。
对于可能 运行 跨越此 post 的任何人,解决方案是从显式函数调用更改为隐式函数调用,如下所示。
fit('getDesign should append params and return', () => {
const response = {
'success': true,
'data': [{foo: 'bar'}],
'metadata': {'version': '1.8.25', 'versionHash': '14e456e062583f176d25'},
'total': 0,
'message': []
};
doServiceTest('getDesign', response, 42, [{foo: 'bar'}],
'/get-design?designId=42', 'GET');
});
function doServiceTest(myFunction: string, myResponse, myParam, myExpected, myUrl, myMethod) {
service[myFunction](myParam).subscribe((res) => {
expect(res).toEqual(myExpected);
});
const call: TestRequest = httpMock.expectOne(myUrl);
expect(call.request.method).toEqual(myMethod);
call.flush(myResponse);
}