09.
Servicio GitHub: Implementación
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
4 Comments
Wouldn't it be better to use the Symfony\Contracts\HttpClient\HttpClientInterface? If not, what would be the disadvantages?
Yo @S-H!
Do you mean: would it be better to autowire
HttpClientInterfaceas an argument to the constructor of the service instead of instantiating a fresh one inside? If so, then yes :). We actually do it in the next chapter - we just didn't do it here because doing that would require mocking in the test, and we weren't quite ready yet. But in general, allowing it to be passed in is typically better because you can mock it in your tests.Cheers!
Is it ok to write unit test for the service that uses a real API request, as the test can fail if the data will change on the external resource?
Hey @maMykola!
It depends on the situation, but in these situations, sometimes I write ONLY a unit test, sometimes ONLY an integration test, and sometimes both :p. Let's look at it:
A) Suppose I'm using an API that I trust - e.g. Stripe - where there is VERY little chance that they would ever do something silly and accidentally change their API. But, the data I get back from the API is pretty complex and I do some pretty complex stuff with it. In this case, I might only unit test that service: I would mock the API, fake the response, and test that my handling is correct.
B) Now suppose that I'm using an API that I do NOT trust: it's a smaller company, or they have a reputation of doing silly things, or it's some internal thing another company made for you that might just change one day. But, the data I get back from them is very simple and I don't do a lot of processing on it. In this case, I might ONLY do an integration test: I would (if possible) make a test that ACTUALLY hits their API and makes sure that I get back the data I expect. I'm not really testing my code in this case... I'm testing that their API didn't do anything silly :p. But you're totally right that testing an external resource is HARD. Sometimes it's just not feasible. And even if it is, you need to be very careful because, as you said, the data may change on that external resource. And so, I typically make my assertions very "generic". I may assert that the JSON I receive back "has" a certain key but I may not assert the value of that key, as it may change.
And if I have a combination of (A) and (B), I might have both a unit and integration test. I guess I'm realizing that (A) a unit test (where you mock the API) is a way for you to test YOUR logic of what you do with the data from the API. And (B) an integration test (where you use the real API) is a way for you to test that your assumptions about what the API will return are correct.
Cheers!
"Houston: no signs of life"
Start the conversation!