Laravel Movie App - Testing API - Part 3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys welcome back and in this video I like to test our application so our app is fairly simple and it's just making use of an API so we'll take a look at testing our API and faking responses so right now the way I look at it our app only has two features you can view this page here with an index of movies and you can view a single movie page with movie details so let's go ahead and write our feature tests to match those two features so let's go into our code and let's make use of the default test here in tests feature and then rename this too let's rename it to view movies test you movies test and let's go ahead and change this u movies test okay and I like to use in a case with an annotation and I have a stable for that and let's call this test the main page shows correct info so let's go ahead and make a response variable here and let's hit this route I like to use my name routes in here so movies dot index okay and let's just see if our response is successful so assert successful or you can do assert status 200 or assert okay as well so this will pass hopefully it's safe passage okay and it does and let's just make a few more assertions here so lets assert that we see popular movies and I'm okay with that for now so let's just say response assert C or assert C text talk to you popular movies and again this should pass because that should be in the response okay the problem with this is that we are hitting our API endpoints in general when you're testing you don't want to hit external services whether that's an API or sending out mail or something like that so to prove to you that we're hitting the API let's go ahead and go into our movies controller and right here I am just going to die and dump the popular movies okay and I am just gonna die in them each JSON without the results key here and I'll put that back later and if I hit this again this is the response from the API so it has all the movie information from the tmdb API so like I said we don't want to hit that end point in our tests so what we can do is fake it so Larry Bell has a bunch of Handy fakes for many different features such as Mela bowls queues events notifications and for the HTTP client as well so if you go into the documentation and go to faking responses you'll see how to do that so if we do HTTP fake before our test then we can specify our own fake responses so let me show you that back to our test and if we do this at the top or anywhere before we hit or make a response then you'll see that now I run this I got an error after import okay let's try it again and now you see it's no because we're faking it so if you look at the docs we can actually fake specific URLs right here and we can provide our own response so what we want to do is basically take the response or a similar response to what we get from the real API and fake it in our own application so let me show you what I mean back to our app let me take this out for a second and it's run this again so what I want to do is specify this entire array I don't know if it goes that far up okay it does so I'm going to copy all of this is that the right thing okay and what we want to do is mimic that response in here in our own application so let's do that so let's put the fake back and we want to specify our own responses in here so in the controller we're hitting three endpoints here and we want to hit those three endpoints in here as well by hit I mean fake so let's put the exact URL here that we want to fake so API that the movie DB the org slash three slash movie slash popular so that's the first one and the response goes in here or our fake response so if you look at the docs it's gonna be an HTTP response okay so let's do that HTTP response and then the first argument is the fake response so the thing I copied one should go in here and the second argument is the HTTP status so that thing I copied should go in here and this is a huge array and obviously this is not exactly a PHP array so you're gonna have to spend the time to make this a real PHP array and you can change the data if you want so I'll do that behind the scenes because this is a really cumbersome process so if that wasn't entirely clear you'll see now that I have a response here and I have a key of results and a value of foo and you should see that in here when I run my test there we go so here is the fake response we have here and as you can see I only have one result here and I named the movie fake movie so now we can assert that we see this fake movie in our fake response so we want to do this for the other two endpoints that we're hitting - so for these two and this can get quite messy so what I'm gonna do is extract them two methods so we'll return this response in a method so let me just copy this and we can just cut it and we'll make a method here called fake popular movies and then we'll do two more here for the other two end points so I'm just comment that out we'll put a meth in here and I called it fake popular movies and takes no parameters paste that in actually let's return and then we can paste at it so I'm gonna do the same for the other two end points just repeat the same process and if you want you can inspect the real response here by just dying and dumping and then you can just copy that and fake the array okay so now I have my fake now playing movies and my fake genres as well and for my fake now playing movies the movie title here is now playing fake movie and like I said earlier the fake popular movie is the only movie I have in there is called fake movie so now we can assert that we see that text in the response from our fake responses here so let me just put this back to what it was before and now if I did everything correctly this should work so fake movie is the fake movie we have in our popular movies and for the genres this is exactly what's coming back from the API so let's just grab one from our actual app here and there's a assert that we see it so let's grab this one actually not what am I talking about we have to see what the genres are for this one so I have these 12 18 and so on and so forth so we have to match these genres with the actual genres and I believe I do have that so it's gonna paste that in so those are the five genres corresponding to those genre IDs actually I'm gonna leave that one out for now and let's do the now playing movies as well okay so this should be now playing and this is called now playing fake movie like I showed you that's not gonna go okay so now if I did everything correctly let's run the test and me over there semicolon okay so now our test is working and you can see it's much faster because we're faking the responses instead of hitting the actual API so let's fix that genres let's see if this is probably gonna fail and let's see why okay so I can't find this and I'll show you why so if you go into our actual app and if you inspect this in dev tools and if just open that up you'll see that it's rendering the white space here and we're inspecting the response and not be rendered text in the browser so we have to format this so there's no white space so to me that feels a little brittle let's go to a movie card let me show you what I mean so we have to format this so that there's no line breaks and no white space and like I said that seems a bit brittle to me I don't want to have to format this in a way just so that I can make the test pass but that's the only way I know how to do it for now so let's try to get it passing so we want no spaces here and this is a wrapping line it's on the same line just my editor wraps and let's see if this gets rid of the whitespace so let's refresh yes back this okay there's still some white space here and it looks like there's two spaces in between comments so it's a bit that Dave here so yeah I just got rid of the space before the end if and now there's only one space no this should pass and it does awesome so yeah that seems brittle to me if you have a better solution please let me know okay so now let's write our test for the other feature which is just a single movie here so pretty much the same thing let's go back to our test and it's make another one down here let's call it oops the movie page shows the correct info and I'm gonna make you watch me do this I'll just talk through it so we're faking that end point that we're hitting and I have started here for a wild card because if I go down here to show I wanted to match any ID here and just like earlier I have this fake response for the single movie which I am going to paste in right here so the name of that fake movie is when it to fake Jumanji I think yes right here fake Jumanji so that's what we're gonna assert against and these are in the fake response as well so if I did everything correctly this test should pass as well and it does cool and if you want to run our whole test suite we can do phpunit and it runs really quickly because there's only two tests one of the default ones in there too so let's get rid of that this one creates application yeah it's Kara this sorry that creates application there's a unit test in here too so I guess we'll just leave that in there or a PHP PHP in level seven there's a new PHP artisan test command which has nicer output if you want to do that so we successfully tested our application specifically we faked our API responses and made assertions against our fake responses please like comment and subscribe if you haven't already done so thanks for watching guys see you in the next one okay thanks bye [Music]
Info
Channel: Andre Madarang
Views: 10,571
Rating: undefined out of 5
Keywords: laravel http, http laravel, laravel http client, laravel movie app, laravel client, laravel zttp, laravel ajax, movie app laravel, andre madarang, drehimself, laravel tmdb, movie api, laravel api, laravel http api, tmdb api, laravel ajax calls, laravel 7 new features, laravel 7 http client, laravel 7 http, laravel 7 blade components, laravel blade component, laravel http testing, laravel fake, laravel 7 http fake, laravel testing, laravel tdd, laravel testing api
Id: WB5X3y9Btm4
Channel Id: undefined
Length: 13min 44sec (824 seconds)
Published: Mon Mar 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.