Robin Dorstijn
Robin Dorstijn

Reputation: 101

How can I write unit tests for angular's HttpClient with Jasmine Marbles?

I have a project where I extensively use angular HttpClient requests as a basis for streams. I would love to test these requests using jasmine marbles, as it seems to be 'industry standard' for unit testing of streams.

I am however struggling with getting the whole thing to work, either the HttpTestingController fails, or the marble fails. I have built a stack blitz to demonstrate my point.

Upvotes: 2

Views: 1093

Answers (1)

Jan-Niklas Wortmann
Jan-Niklas Wortmann

Reputation: 421

I am part of the RxJS Core Team and I would not recommend you using jasmine marbles or marble testing in general to test business logic. I never needed to use those. I'd highly recommend you using the HttpClientTestingModule for this. It's way easier to maintain.

Marble Testing is very good if you are implementing your own operators or you want to test edge cases like subscription and unsubscription-behavior, effects on the source observable and so on.

I forked your stackblitz and changed one of the test cases the way I would do it: https://stackblitz.com/edit/angular-unit-tests-8fqyl1?file=src/testing/http-client.spec.ts

Upvotes: 6

Related Questions