如何在服务中测试JSONP方法-Angular [英] How to test a JSONP method in a service - Angular
问题描述
我正在制作一个简单的电子邮件订阅应用程序,为此我已经编写了测试,并且到目前为止一切正常.
I am making a simple email subscription application for which I have written tests and things went fine as for now.
为了使事情变得简单,我想解释一下我所面对的问题的确切部分.
In order to make things simple, I wish to explain the exact part where I am facing the issue.
my-service.ts :
export class MyService {
mailChimpEndpoint =
'https://gmail.us10.list-manage.com/subscribe/post-json?u=aaa7182511d7bd278fb9d510d&id=01681f1b55&';
constructor(private _http: HttpClient) {}
submitForm(email: string){
const params = new HttpParams()
.set('EMAIL', email)
.set('subscribe','Subscribe')
.set('b_aaa7182511d7bd278fb9d510d_01681f1b55','')
const mailChimpUrl = this.mailChimpEndpoint + params.toString();
return this._http.jsonp<MailChimpResponse>(mailChimpUrl, 'c')
}
}
上面是调用api的服务中的简单方法
The above is a simple method in a service that calls an api
链接到服务文件: https://stackblitz.com/edit/angular-testing-imniaf?file=app%2Fmy-service.ts
现在,我正在尝试为上述方法submitForm
中的一个编写测试用例
Now I am trying to write a test case for the above method submitForm
in
my-service.spec.ts
it('check subscribeEmail function has been called in service', (done: DoneFn) => {
const service = TestBed.get(MyService);
service.submitForm('test@gmail.com').subscribe(
response => {
expect(response).toEqual(mockServiceData);
}
)
const reqMock = httpTestingController.expectOne(request => request.url === mailChimpEndpoint);
expect(reqMock.request.method).toBe('GET');
reqMock.flush(mockServiceData);
});
此规范文件的链接: https ://stackblitz.com/edit/angular-testing-imniaf?file = app%2Fmy-service.spec.ts
这是我被卡住的地方,上面的代码将错误抛出为,
This is where I am getting stucked and the above code throws the error as,
错误:对于标准按功能匹配: ",一无所获.
Error: Expected one matching request for criteria "Match by function: ", found none.
完整的工作stackblitz链接:
The complete working stackblitz link:
请帮助我通过此测试,作为成功.
Please help me to pass this test as success.