问题描述
我正在制作一个简单的电子邮件订阅应用程序,为此我已经编写了测试,并且到目前为止一切正常.
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')
}
}
链接到服务文件: 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('[email protected]').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,
完整的工作stackblitz链接:
The complete working stackblitz link:
请帮助我通过此测试,作为成功.
Please help me to pass this test as success.
推荐答案
-
您应该删除了
useClass
:)
已更正的mailChimpEndpoint
值.
已更正的expect(reqMock.request.method).toBe('JSONP');
已删除done: DoneFn
.
it('check subscribeEmail function has been called in service', () => {
const service = TestBed.get(MyService);
service.submitForm('[email protected]').subscribe(
response => {
console.log(response)
expect(response).toEqual(mockServiceData);
}
)
const reqMock = httpTestingController.expectOne(req => req.url === mailChimpEndpoint);
expect(reqMock.request.method).toBe('JSONP');
reqMock.flush(mockServiceData);
});
这篇关于如何在服务中测试JSONP方法-Angular的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!