2
Http
サービスで、正しいエンドポイントを呼び出すかどうかを偵察したいと思います。それは可能ですか?Angular2 Jasmine spyOn http calls
これまでのところ、私のサービスでは、私がテストしているのは、彼らがデータを返信していることです...私はそれらをより有用にしたいと思います。ほとんどが
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)
);
}));
});
うわー、私は仕事で一週間オフに行って、完全に(私はあきらめて、それをテストしないことに決めていた)そのポストについて忘れてしまいました。あなたの解決策はうまくいく、それはすごい!あなたにありがとうおい – trichetriche