11.
Mocking: Stubs
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.
14 Comments
If the project involves developing a RESTful API and we plan to make requests to our own API, it would be ideal to have functional tests with fake fixtures, or having unit tests (using mocks), or both types of tests.
Hey @ahmedbhs ,
Yeah, agree! Actually, no matter if you're gonna make requests to your own API or it will be only your users - those tests would be good to have. It mostly may depend on the specific API (project) and how complex that is.
Cheers!
Hello, thanks for this course. Can you explain why tests are accepted with wrong title and label?
Hello @Yacintosh
At this moment we do not test if data is correct, you will see it in the next chapters and there will be a test to cover this situation
Cheers!
A very nice course.
One Point: A Service-Class should be stateless per definition. Symfony's HTTP-Client is stateful. When storing it in a class-member, the whole service-class isn't stateless anymore.
Hey @lid
Good point, but I don't think it's a problem as long as your service class serves only one API, because as far as I know, the HTTP client only holds configuration, so you can reuse that object to perform different HTTP requests to the same API
Cheers!
Hi,
great tutorial so far, quite enjoyable.
I've got a question related to testing response from real service.
In this case, we won't call github api because we don't control it and our test fails if it's unavailable, for example.
Mocking the Response will make sure tests are going to pass but the application won't have the correct status.
For example, the API is unavailable or changed its array structure, tests will pass but I'll never find out the application is broke (it won't be able to get health status from github).
My question is: is there a way to test the scenario?
thanks
Hey @Felipe-L
In that case, you'd have to make a real API call to GitHub. I don't see a value in doing a health check of the API on your tests because that's outside of your control, it's better to have a retry mechanism or the ability to tolerate failures (disable the feature on the frontend)
Cheers!
Hi @MolloKhan, thanks for your reply.
I got what you said and agree that's not on our control to test an API call.
Lets say my application depends on this feature to work properly. Would be a good idea to mark the test as skipped if it's not possible to return the content from the API?
Then have another mechanism to let us know that the API is unavailable?
thanks
I think we're talking about two things.
1) Testing a real API response to be aware of changes. In this case, your test will need to make a real request to the API, that's sometimes fine, but keep in mind that those tests are slow, so you want to have just a handful of them
2) API health status. You can add a test where you mock out the response, and it will always return an error so you can test how your application behaves in that circumstance. For example, you could show a warning message on the frontend
Also, as I said earlier you can add a retry mechanism. This can be easily integrated with Symfony Messenger
Cheers!
What's up with all the !!!!!!?
use.....!!!!!!!!!!!!!!!!!!! And, for the test, we'll pretend she sprained her ankle so add a labels key set to an array, that includes 'name' => 'Status: Sick'
!!!!!!!!!! Zoom back to the Github Issues to "circling" Maverick!!!!!!!!!!!!!!!!!!! Let's also cr...
Hey Tac!
Whoops, those are some dev comments we used when had been creating this tutorial. I fixed it in https://github.com/SymfonyCasts/testing/commit/2248a2b70b0fad08f2077271d6c3f4d7829d2473 , thank you for reporting it :)
Cheers!
There is another instance of this.
!!!!!!!!! This chapter is short enough, we could? run the tests !!!!!!!!!
around 1:22 min of the video
Ah! The script was fixed, but the subtitles need to be regenerated. We'll get on that! Thanks for the note :)
"Houston: no signs of life"
Start the conversation!