Angular2 Jasmine spyOn http 调用
Angular2 Jasmine spyOn http calls
我想监视 Http
服务,看看它是否调用了正确的端点。有可能这样做吗?
到目前为止,在我的服务中,我测试的只是它们发回数据……我想让它们更有用。
如果您需要它,这是我当前的规范文件:
import { TestBed, async, inject } from '@angular/core/testing';
import { SignupService } from './signup.service';
import { Observable } from 'rxjs/Observable';
import { HttpModule, Http, Response, ResponseOptions, RequestOptions, Headers, XHRBackend } from '@angular/http';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { ErrorHandlerService } from '../../services/error-handler.service';
describe('SignupService', () => {
let errorStub = {};
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [
SignupService,
{ provide: XHRBackend, useClass: MockBackend },
{ provide: ErrorHandlerService, useValue: errorStub }
]
});
});
it('signup should return an application error if no apps are provided', inject([SignupService], (service: SignupService) => {
service.signup('', [], null).subscribe(
suc => { },
err => expect(err).toMatch(/application/)
);
}));
it('signup should return a role error if no roles are provided', inject([SignupService], (service: SignupService) => {
service.signup('', null, []).subscribe(
suc => { },
err => expect(err).toMatch(/rôle/)
);
}));
it(
'signup should return a response with a "contenu" parameter',
inject([SignupService, XHRBackend], (service: SignupService, mockBackEnd: MockBackend) => {
let content = 'anything';
mockBackEnd.connections.subscribe((connection: MockConnection) => {
connection.mockRespond(new Response(new ResponseOptions({
body: JSON.stringify({
contenu: content
})
})));
});
service.signup('', [], []).subscribe(
suc => expect(suc).toBe(content)
);
}));
it(
'checkUser should return a response with a "contenu" parameter',
inject([SignupService, XHRBackend], (service: SignupService, mockBackEnd: MockBackend) => {
let content = 'anything';
mockBackEnd.connections.subscribe((connection: MockConnection) => {
connection.mockRespond(new Response(new ResponseOptions({
body: JSON.stringify({
contenu: content
})
})));
});
service.checkUser('').subscribe(
suc => expect(suc).toBe(content)
);
}));
});
快到了!只需在 mockBackend 声明中添加 expect 即可。
示例:
mockBackend.connections.subscribe(connection => {
// Check if it invokes a http POST call
expect(connection.request.method).toBe(RequestMethod.Post);
// Check if the URL is correct
expect(connection.request.url).toBe('/some-url');
connection.mockRespond(new Response(new ResponseOptions({
body: JSON.stringify(''),
status: 200
})));
});
我想监视 Http
服务,看看它是否调用了正确的端点。有可能这样做吗?
到目前为止,在我的服务中,我测试的只是它们发回数据……我想让它们更有用。
如果您需要它,这是我当前的规范文件:
import { TestBed, async, inject } from '@angular/core/testing';
import { SignupService } from './signup.service';
import { Observable } from 'rxjs/Observable';
import { HttpModule, Http, Response, ResponseOptions, RequestOptions, Headers, XHRBackend } from '@angular/http';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { ErrorHandlerService } from '../../services/error-handler.service';
describe('SignupService', () => {
let errorStub = {};
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [
SignupService,
{ provide: XHRBackend, useClass: MockBackend },
{ provide: ErrorHandlerService, useValue: errorStub }
]
});
});
it('signup should return an application error if no apps are provided', inject([SignupService], (service: SignupService) => {
service.signup('', [], null).subscribe(
suc => { },
err => expect(err).toMatch(/application/)
);
}));
it('signup should return a role error if no roles are provided', inject([SignupService], (service: SignupService) => {
service.signup('', null, []).subscribe(
suc => { },
err => expect(err).toMatch(/rôle/)
);
}));
it(
'signup should return a response with a "contenu" parameter',
inject([SignupService, XHRBackend], (service: SignupService, mockBackEnd: MockBackend) => {
let content = 'anything';
mockBackEnd.connections.subscribe((connection: MockConnection) => {
connection.mockRespond(new Response(new ResponseOptions({
body: JSON.stringify({
contenu: content
})
})));
});
service.signup('', [], []).subscribe(
suc => expect(suc).toBe(content)
);
}));
it(
'checkUser should return a response with a "contenu" parameter',
inject([SignupService, XHRBackend], (service: SignupService, mockBackEnd: MockBackend) => {
let content = 'anything';
mockBackEnd.connections.subscribe((connection: MockConnection) => {
connection.mockRespond(new Response(new ResponseOptions({
body: JSON.stringify({
contenu: content
})
})));
});
service.checkUser('').subscribe(
suc => expect(suc).toBe(content)
);
}));
});
快到了!只需在 mockBackend 声明中添加 expect 即可。
示例:
mockBackend.connections.subscribe(connection => {
// Check if it invokes a http POST call
expect(connection.request.method).toBe(RequestMethod.Post);
// Check if the URL is correct
expect(connection.request.url).toBe('/some-url');
connection.mockRespond(new Response(new ResponseOptions({
body: JSON.stringify(''),
status: 200
})));
});