We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hi, it's been a while! 😄 How are you?
I'm trying to test an HttpInterceptorFn. There isn't much official Angular documentation on this, and there are no examples on your site.
HttpInterceptorFn
I found something interesting thanks to this article:
MockBuilder() .provide(provideHttpClient(withInterceptors([myInterceptor]))) .provide(provideHttpClientTesting())
The issue is that provideHttpClient returns an EnvironmentProviders, and provide only accepts Provider.
provideHttpClient
EnvironmentProviders
provide
Provider
It would be helpful to check if provide can accept EnvironmentProviders and update the typing.
In the meantime, this works:
MockBuilder() .provide(provideHttpClient(withInterceptors([myInterceptor])) as unknown as Provider) .provide(provideHttpClientTesting())
Here's a complete example in case you want to add it to your documentation:
// my.interceptor.ts export const myInterceptor: HttpInterceptorFn = <D = unknown>(request: HttpRequest<Response<D>>, next: HttpHandlerFn) => { return next(request); }; // my.interceptor.spec.ts describe('myInterceptor', () => { let client: HttpClient; let httpMock: HttpTestingController; beforeEach(() => MockBuilder() .provide(provideHttpClient(withInterceptors([myInterceptor])) as unknown as Provider) .provide(provideHttpClientTesting()) ); beforeEach(() => { client = ngMocks.findInstance(HttpClient); httpMock = ngMocks.findInstance(HttpTestingController); }); it('should blablabla', () => { client .get('/test') .pipe(first()) .subscribe((response) => { expect(response).toEqual('data'); }); const req = httpMock.expectOne('/test'); req.flush('data'); httpMock.verify(); }); });
Have a great day. ❤️
The text was updated successfully, but these errors were encountered:
feat(MockBuilder): supports EnvironmentProviders help-me-mom#7011
b3ca1d4
Hi @kekel87,
thanks for noting about that. The support will be released with the next version.
Sorry, something went wrong.
Merge pull request #7441 from satanTime/issues/7011
1344edb
feat(MockBuilder): supports EnvironmentProviders #7011
v14.12.0 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems.
satanTime
Successfully merging a pull request may close this issue.
Hi, it's been a while! 😄 How are you?
Describe the feature or problem you'd like to solve
I'm trying to test an
HttpInterceptorFn
. There isn't much official Angular documentation on this, and there are no examples on your site.I found something interesting thanks to this article:
The issue is that
provideHttpClient
returns anEnvironmentProviders
, andprovide
only acceptsProvider
.Proposed solution
It would be helpful to check if
provide
can acceptEnvironmentProviders
and update the typing.In the meantime, this works:
Additional context
Here's a complete example in case you want to add it to your documentation:
Have a great day. ❤️
The text was updated successfully, but these errors were encountered: