Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Mocking backend responses #279

Unanswered
meirka asked this question in Q&A
Feb 2, 2022 · 3 comments · 1 reply
Discussion options

Hello, is there a way to mock backend responses?

In case of TestBed, when I have a component with service that calls backend, I use HttpTestingController to respond to a request. How do I go about it with testing library?

You must be logged in to vote

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
1 reply
Comment options

Comment options

You can mock HttpClient using a spy object.

e.g. using Jasmine:

 import { HttpClient } from '@angular/common/http';
 const httpClient: jasmine.SpyObj<HttpClient> = jasmine.createSpyObj('HttpClient', ['post']);
 httpClient.post.and.returnValue('Mocked response');
 // TODO: Provide httpClient on Angular Testing Library providers array.
 // { provide: HttpClient, useValue: httpClient }
You must be logged in to vote
0 replies
Comment options

You can use the injector on the debugElement of the object, the render function returns.

const renderResult = await render(YourComponent, {
 providers: [provideHttpClient(), provideHttpClientTesting()],
})
const http = renderResult.debugElement.injector.get(HttpTestingController);
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /