如何在服务中测试JSONP方法-Angular [英] How to test a JSONP method in a service - Angular

查看:67
本文介绍了如何在服务中测试JSONP方法-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&amp';

  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:

https://stackblitz.com/edit/angular-testing-ajoov8

请帮助我通过此测试,作为成功.

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('test@gmail.com').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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆