12.
Mocking: Mock Objects
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.
13 Comments
Hi, I noticed in the test after the chapter that the question was probably created before one of the last updates in phpUnit where they added the
createStub()method and actually made a distinction between mocks and stubs. Just in case you want to update that, I suppose that if I didn't know, it could have been confusing when working with phpUnit later.Hey @Fernando
Thank you for the heads up! We'll definitely update the challenge. Cheers!
Is there a relation between ->with() and ->willReturn() ?
Reading:
...->with('GET', 'https://api.github.com/repos/SymfonyCasts/dino-park/issues')
->willReturn($mockResponse);
The response will not actually return the $mockResponse, are those two calls in the chain independent? Reading it seems confusing. I guess willReturn() is more of a statement than an expectation, right?
Hey @Alkiviadis-D
The
willReturn()method is not an expectation but a way to configure your mocks to return specific data. And, thewith()method allows you to define with what input arguments the method should be called.I hope it helps clarify things a bit. Cheers!
Within the context of this test, aren't we testing implementation instead of behavior if we check that only one request is made?
Howdy!
Sort of... In order for
GithubServiceto work, it needs to make the API call to GitHub. To do that, we tell PHPUnit that hey,request()must be called X number of times - in our case,once(). If we didn't have theexcepts()assertion, then the test would still pass as seen in the previous chapter.The same applies to the
with()assertion - ifrequest()is called with anything other thanGET&&https://api.github.com/repos/SymfonyCasts/dino-parkthe test will fail.In most cases, when you mock and configure a service (or any other object), you want to ensure that the method is called and has the required arguments, if any. Another way to think about it - if we were going to deploy this app to production and the cost of making API calls was a factor - having a test in place to ensure that we're are calling the correct API and only calling it once, would be pretty crucial...
I hope you're enjoying the series and if I misunderstood your question, please let us know!
Thanks!
Since the client is mocked, I don't understand how it validates that it uses GET with the correct URL. Can you please explain?
Hey Julien,
Good question! The easiest way would be to change GET with POST in the test and run the test - you should see it that it will fail this time. So, it's happening in PHPUnit, with the
with()method you tell PHPUnit to make sure that the method is called with the exact arguments you're passing in the test, i.e.with('GET', 'https://api.github.com/repos/SymfonyCasts/dino-park')and PHPUnit take care of the rest, i.e. validates that the args are exactly like you mentioned and throws if they are not :)So, in short, PHPUnit validates it on the mocked object, not a real object.
I hope it's clearer for you now.
Cheers!
Something does not work: - The media could not be loaded, either because the server or network failed or because the format is not supported.
Howdy! Are you seeing that error when attempting to play the video?
Yes.
But, I see it works now.
Awesome! It sounds like there was a slight network hiccup. We hope you're enjoying the series and learning a thing or two along the way.
It's very helpfull series I hope we will see about others tests too.
Thank you.
"Houston: no signs of life"
Start the conversation!