如何在服务中测试 JSONP 方法 - Angular
How to test a JSONP method in a service - Angular
我正在制作一个简单的电子邮件订阅应用程序,我已经为其编写了测试,目前一切正常。
为了简单起见,我想解释一下我遇到问题的确切部分。
我的-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')
}
}
The above is a simple method in a service that calls an api
Link 到服务文件:https://stackblitz.com/edit/angular-testing-imniaf?file=app%2Fmy-service.ts
现在我正在尝试为上述方法submitForm
编写一个测试用例
我的-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);
});
Link 到此规范文件:https://stackblitz.com/edit/angular-testing-imniaf?file=app%2Fmy-service.spec.ts
这是我卡住的地方,上面的代码抛出错误,
Error: Expected one matching request for criteria "Match by function:
", found none.
完整的工作 stackblitz link:
请帮助我成功通过这个测试。
HttpClient
应该是间谍,那你就断言吧
TestBed.configureTestingModule({
// add this to the definition of the testing module
providers: [{
provide: HttpClient,
useValue: {
jsonp: jasmine.createSpy('httpClient.jsonp'),
}
}]
})
然后在测试
it('check subscribeEmail function has been called in service', (done: DoneFn) => {
const service = TestBed.get(MyService);
const httpClient = TestBed.get(HttpClient);
const expected: any = Symbol();
(httpClient.jsonp as any).and.returnValue(expected);
const actual = service.submitForm('test@gmail.com');
expect(actual).toBe(expected);
expect(httpClient.jsonp).toHaveBeenCalledWith(provideValueHere, 'c');
});
现在你的测试应该是这样的:
import { HttpClient } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';
import { MyService } from './my-service';
describe('MyService', () => {
beforeEach(() => TestBed.configureTestingModule({
providers: [
MyService,
{
provide: HttpClient,
useValue: {
jsonp: jasmine.createSpy('httpClient.jsonp'),
}
}
]
}).compileComponents());
it('check subscribeEmail function has been called in service', () => {
const httpClient = TestBed.get(HttpClient);
const service = TestBed.get(MyService);
const expected: any = Symbol();
(httpClient.jsonp as any).and.returnValue(expected);
const actual = service.submitForm('test@gmail.com');
expect(actual).toBe(expected);
expect(httpClient.jsonp).toHaveBeenCalledWith('https://gmail.us10.list-manage.com/subscribe/post-json?u=aaa7182511d7bd278fb9d510d&id=01681f1b55&EMAIL=test@gmail.com&subscribe=Subscribe&b_aaa7182511d7bd278fb9d510d_01681f1b55=', 'c');
});
});
你应该删除 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);
});
我正在制作一个简单的电子邮件订阅应用程序,我已经为其编写了测试,目前一切正常。
为了简单起见,我想解释一下我遇到问题的确切部分。
我的-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')
}
}
The above is a simple method in a service that calls an api
Link 到服务文件:https://stackblitz.com/edit/angular-testing-imniaf?file=app%2Fmy-service.ts
现在我正在尝试为上述方法submitForm
编写一个测试用例
我的-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);
});
Link 到此规范文件:https://stackblitz.com/edit/angular-testing-imniaf?file=app%2Fmy-service.spec.ts
这是我卡住的地方,上面的代码抛出错误,
Error: Expected one matching request for criteria "Match by function: ", found none.
完整的工作 stackblitz link:
请帮助我成功通过这个测试。
HttpClient
应该是间谍,那你就断言吧
TestBed.configureTestingModule({
// add this to the definition of the testing module
providers: [{
provide: HttpClient,
useValue: {
jsonp: jasmine.createSpy('httpClient.jsonp'),
}
}]
})
然后在测试
it('check subscribeEmail function has been called in service', (done: DoneFn) => {
const service = TestBed.get(MyService);
const httpClient = TestBed.get(HttpClient);
const expected: any = Symbol();
(httpClient.jsonp as any).and.returnValue(expected);
const actual = service.submitForm('test@gmail.com');
expect(actual).toBe(expected);
expect(httpClient.jsonp).toHaveBeenCalledWith(provideValueHere, 'c');
});
现在你的测试应该是这样的:
import { HttpClient } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';
import { MyService } from './my-service';
describe('MyService', () => {
beforeEach(() => TestBed.configureTestingModule({
providers: [
MyService,
{
provide: HttpClient,
useValue: {
jsonp: jasmine.createSpy('httpClient.jsonp'),
}
}
]
}).compileComponents());
it('check subscribeEmail function has been called in service', () => {
const httpClient = TestBed.get(HttpClient);
const service = TestBed.get(MyService);
const expected: any = Symbol();
(httpClient.jsonp as any).and.returnValue(expected);
const actual = service.submitForm('test@gmail.com');
expect(actual).toBe(expected);
expect(httpClient.jsonp).toHaveBeenCalledWith('https://gmail.us10.list-manage.com/subscribe/post-json?u=aaa7182511d7bd278fb9d510d&id=01681f1b55&EMAIL=test@gmail.com&subscribe=Subscribe&b_aaa7182511d7bd278fb9d510d_01681f1b55=', 'c');
});
});
你应该删除
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);
});