1 00:00:01,170 --> 00:00:03,750 In this video we're gonna move on to our next request handler. 2 00:00:03,750 --> 00:00:06,870 We're gonna work on this kind of show route right here. 3 00:00:06,870 --> 00:00:12,540 So this route we're going to make a request to API tickets and then the idea of a very particular ticket 4 00:00:12,600 --> 00:00:14,600 that has already been saved to our database. 5 00:00:14,730 --> 00:00:19,530 We should then attempt to find that tick inside the database and return it naturally if we cannot find 6 00:00:19,530 --> 00:00:19,970 that ticket. 7 00:00:19,980 --> 00:00:22,310 We'll respond with a forum for air. 8 00:00:22,350 --> 00:00:25,570 We're going to once again do a kind of test driven development approach. 9 00:00:25,590 --> 00:00:27,690 We're not really following test driven development here. 10 00:00:27,690 --> 00:00:32,220 Like in the strict sense we should really call it a test first approach just to be a little bit more 11 00:00:32,220 --> 00:00:33,540 precise. 12 00:00:33,540 --> 00:00:38,070 So I have to find my roots directory off on the test folder inside there and inside that I'll make a 13 00:00:38,070 --> 00:00:39,970 new file called show test. 14 00:00:40,070 --> 00:00:46,130 T.S. will then import everything needed to actually test out this new root handler. 15 00:00:46,230 --> 00:00:58,400 So I will import request from Super Test and my express app from up one directory up another app. 16 00:00:58,410 --> 00:01:02,310 All right I'm then going to write out to different test cases because this thing is going to be pretty 17 00:01:02,310 --> 00:01:03,730 straightforward and functionality. 18 00:01:03,920 --> 00:01:07,980 I'm going to first check and make sure that if we do not find the ticket that we're looking for we return 19 00:01:08,010 --> 00:01:09,650 a status of 4 or 4. 20 00:01:09,750 --> 00:01:12,040 So say it's returns a 4 or. 21 00:01:12,210 --> 00:01:13,980 If the ticket is not found 22 00:01:17,010 --> 00:01:23,430 that's case number one then case number two it returns the ticket if the ticket is found 23 00:01:27,490 --> 00:01:31,850 so it can now start to write out the implementation for both these and the first one I'll do and a wait 24 00:01:31,990 --> 00:01:40,470 request to the app we want to do a GET request to API tickets and then remember whatever we put right 25 00:01:40,470 --> 00:01:43,290 here is the idea the ticket that we're trying to look up. 26 00:01:43,320 --> 00:01:48,130 So in this case I want to make sure that I'm making a request to a ticket that definitely does not exist. 27 00:01:48,130 --> 00:01:50,850 So I got to put in some really fake looking I.D. like so 28 00:01:55,080 --> 00:02:04,520 after that let's go ahead and send the request off and then I will expect to get a 4 4 status code in 29 00:02:04,520 --> 00:02:05,560 response. 30 00:02:05,900 --> 00:02:10,610 Now as a quick reminder if we run this test right now it is going to pass but it's going to pass because 31 00:02:10,610 --> 00:02:15,070 we do not even have a root handler defined for this route just yet. 32 00:02:15,080 --> 00:02:19,520 So right now it's erroneous pass we're going to want to make sure that after we actually defined a request 33 00:02:19,520 --> 00:02:25,820 handler it does truly give us back a forum for let's also take care of the second test just right now 34 00:02:25,820 --> 00:02:27,240 just get all done with. 35 00:02:27,290 --> 00:02:32,180 There's two ways we can really write this test out in order to test the thing we need to make sure that 36 00:02:32,180 --> 00:02:35,920 there is some ticket inside the database before we attempt to fetch it. 37 00:02:36,140 --> 00:02:39,830 And really there's two ways to make sure there is a ticket inside the database. 38 00:02:39,830 --> 00:02:45,290 We can either attempt to access the ticket model directly from inside this test and do something like 39 00:02:45,290 --> 00:02:50,030 ticket dot build with blah blah blah some data and then ticket dot safe. 40 00:02:50,030 --> 00:02:51,340 That's method number one. 41 00:02:51,350 --> 00:02:57,500 The other thing we could possibly do is make a request to build the ticket on the fly so we can make 42 00:02:57,500 --> 00:03:03,320 a request to create the ticket and then attempt to fetch it right after I personally like option number 43 00:03:03,320 --> 00:03:09,310 two because it really stimulates us using our API directly we can use Option number one right here. 44 00:03:09,330 --> 00:03:14,030 Without doubt totally valid and some people will tell you that this is the better way to do it because 45 00:03:14,030 --> 00:03:19,640 in option number two we are technically kind of we kind of have a test here that is testing some other 46 00:03:19,640 --> 00:03:22,480 code that we're not really directly trying to test. 47 00:03:22,580 --> 00:03:23,480 So somebody will tell you. 48 00:03:23,480 --> 00:03:25,280 Option or two is not great. 49 00:03:25,370 --> 00:03:29,600 However I think it's just kind of represents reality a little bit more so I'm going to go with option 50 00:03:29,600 --> 00:03:34,130 number two so in other words we're going to make a request over to that same row handler. 51 00:03:34,150 --> 00:03:35,930 We just put together a moment ago. 52 00:03:36,070 --> 00:03:43,600 So I will do and await request to you at I'm gonna do a post to API tickets. 53 00:03:43,790 --> 00:03:48,290 I'm gonna make sure I set the cookie on there because we have to be authenticated to make the request 54 00:03:53,220 --> 00:03:57,220 I'll then send along some valid title. 55 00:03:57,230 --> 00:04:02,030 And once again let's define the title and price ahead of time just to make sure we can do an actual 56 00:04:02,030 --> 00:04:05,060 assertion and make sure that with the ticket we find has the correct data on it. 57 00:04:05,060 --> 00:04:08,900 So up here I'll do a title of concerts. 58 00:04:09,080 --> 00:04:17,510 And how about a price of 20 and I'll send along that title and that price so after creating that I will 59 00:04:17,510 --> 00:04:19,050 still expect to get back to a one. 60 00:04:19,100 --> 00:04:25,220 Again we kind of don't really want to be testing this other route handler inside of the different show 61 00:04:25,220 --> 00:04:31,720 test file but nonetheless I'm gonna make sure that the creation is working as expected now that should 62 00:04:31,720 --> 00:04:33,520 give us back a response. 63 00:04:33,520 --> 00:04:38,350 So I'm going to capture that response and remember in the response we are sending back the entire ticket 64 00:04:38,380 --> 00:04:39,760 after it is created. 65 00:04:39,760 --> 00:04:44,010 So inside that response is the I.D. of the ticket that we just made. 66 00:04:44,170 --> 00:04:49,030 So we can use that I.D. to now do a follow up request and make sure that we can actually match the ticket 67 00:04:49,030 --> 00:04:53,830 that we we're just asking for so I going to make a second request here. 68 00:04:53,870 --> 00:04:57,760 I'm going to call the response ticket response. 69 00:04:57,850 --> 00:04:59,040 I'm going to make the request. 70 00:04:59,120 --> 00:05:03,610 It's going to be a gift and I'm gonna make this request with some back tickets because I want to be 71 00:05:03,610 --> 00:05:10,740 able to take the I.D. out of that response and put it into this your El so there's my interpretation 72 00:05:10,740 --> 00:05:15,500 right there and I'm going to take a look at response dot body dot I.D.. 73 00:05:15,710 --> 00:05:19,540 So that should be the idea the ticket that was just made during the first request. 74 00:05:21,100 --> 00:05:22,810 I'll then send this off. 75 00:05:22,940 --> 00:05:29,590 I'm going to expect to get a two hundred specifically putting that in there to make sure that we do 76 00:05:29,590 --> 00:05:37,240 not get a forum for now we can inspect the response take the data out of there once again and make sure 77 00:05:37,240 --> 00:05:44,070 that it has the correct title and the correct price so we can put in and expect to get response that 78 00:05:44,110 --> 00:05:58,030 body that title and I can expect that to equal title and ticket response body price to equal rights 79 00:05:59,120 --> 00:06:05,760 we go now once again I know writing the tests out ahead of time is just a little bit annoying. 80 00:06:05,790 --> 00:06:10,680 But look at this we've got something to make sure that our root handler works without having even done 81 00:06:10,710 --> 00:06:12,470 any work on the root handler itself. 82 00:06:12,750 --> 00:06:14,970 So once again I don't have to worry about postman. 83 00:06:14,970 --> 00:06:17,580 I don't have to worry about opening my browser or anything like that. 84 00:06:17,580 --> 00:06:21,480 I can just develop my API in isolation. 85 00:06:21,530 --> 00:06:22,850 Let's go back over to our terminal. 86 00:06:22,850 --> 00:06:24,950 Let's make sure that these tests are running. 87 00:06:25,100 --> 00:06:27,770 It looks like returns the ticket if the ticket is found. 88 00:06:27,770 --> 00:06:32,420 We expected a two hundred but we've got a four or totally makes sense because we've not even defined 89 00:06:32,420 --> 00:06:33,430 that row handler. 90 00:06:33,470 --> 00:06:40,010 And again the other test is passing erroneously because we have not defined that route handler at all. 91 00:06:40,010 --> 00:06:46,280 And it's being captured right now by that not found middleware that we wired up back inside the app 92 00:06:46,280 --> 00:06:47,570 file. 93 00:06:47,660 --> 00:06:48,760 So there's our test file. 94 00:06:48,860 --> 00:06:51,850 Quick pause right here and we'll build out our implementation in just a moment.