In this video we're gonna move on to our next request handler.
We're gonna work on this kind of show route right here.
So this route we're going to make a request to API tickets and then the idea of a very particular ticket
that has already been saved to our database.
We should then attempt to find that tick inside the database and return it naturally if we cannot find
that ticket.
We'll respond with a forum for air.
We're going to once again do a kind of test driven development approach.
We're not really following test driven development here.
Like in the strict sense we should really call it a test first approach just to be a little bit more
precise.
So I have to find my roots directory off on the test folder inside there and inside that I'll make a
new file called show test.
T.S. will then import everything needed to actually test out this new root handler.
So I will import request from Super Test and my express app from up one directory up another app.
All right I'm then going to write out to different test cases because this thing is going to be pretty
straightforward and functionality.
I'm going to first check and make sure that if we do not find the ticket that we're looking for we return
a status of 4 or 4.
So say it's returns a 4 or.
If the ticket is not found
that's case number one then case number two it returns the ticket if the ticket is found
so it can now start to write out the implementation for both these and the first one I'll do and a wait
request to the app we want to do a GET request to API tickets and then remember whatever we put right
here is the idea the ticket that we're trying to look up.
So in this case I want to make sure that I'm making a request to a ticket that definitely does not exist.
So I got to put in some really fake looking I.D. like so
after that let's go ahead and send the request off and then I will expect to get a 4 4 status code in
response.
Now as a quick reminder if we run this test right now it is going to pass but it's going to pass because
we do not even have a root handler defined for this route just yet.
So right now it's erroneous pass we're going to want to make sure that after we actually defined a request
handler it does truly give us back a forum for let's also take care of the second test just right now
just get all done with.
There's two ways we can really write this test out in order to test the thing we need to make sure that
there is some ticket inside the database before we attempt to fetch it.
And really there's two ways to make sure there is a ticket inside the database.
We can either attempt to access the ticket model directly from inside this test and do something like
ticket dot build with blah blah blah some data and then ticket dot safe.
That's method number one.
The other thing we could possibly do is make a request to build the ticket on the fly so we can make
a request to create the ticket and then attempt to fetch it right after I personally like option number
two because it really stimulates us using our API directly we can use Option number one right here.
Without doubt totally valid and some people will tell you that this is the better way to do it because
in option number two we are technically kind of we kind of have a test here that is testing some other
code that we're not really directly trying to test.
So somebody will tell you.
Option or two is not great.
However I think it's just kind of represents reality a little bit more so I'm going to go with option
number two so in other words we're going to make a request over to that same row handler.
We just put together a moment ago.
So I will do and await request to you at I'm gonna do a post to API tickets.
I'm gonna make sure I set the cookie on there because we have to be authenticated to make the request
I'll then send along some valid title.
And once again let's define the title and price ahead of time just to make sure we can do an actual
assertion and make sure that with the ticket we find has the correct data on it.
So up here I'll do a title of concerts.
And how about a price of 20 and I'll send along that title and that price so after creating that I will
still expect to get back to a one.
Again we kind of don't really want to be testing this other route handler inside of the different show
test file but nonetheless I'm gonna make sure that the creation is working as expected now that should
give us back a response.
So I'm going to capture that response and remember in the response we are sending back the entire ticket
after it is created.
So inside that response is the I.D. of the ticket that we just made.
So we can use that I.D. to now do a follow up request and make sure that we can actually match the ticket
that we we're just asking for so I going to make a second request here.
I'm going to call the response ticket response.
I'm going to make the request.
It's going to be a gift and I'm gonna make this request with some back tickets because I want to be
able to take the I.D. out of that response and put it into this your El so there's my interpretation
right there and I'm going to take a look at response dot body dot I.D..
So that should be the idea the ticket that was just made during the first request.
I'll then send this off.
I'm going to expect to get a two hundred specifically putting that in there to make sure that we do
not get a forum for now we can inspect the response take the data out of there once again and make sure
that it has the correct title and the correct price so we can put in and expect to get response that
body that title and I can expect that to equal title and ticket response body price to equal rights
we go now once again I know writing the tests out ahead of time is just a little bit annoying.
But look at this we've got something to make sure that our root handler works without having even done
any work on the root handler itself.
So once again I don't have to worry about postman.
I don't have to worry about opening my browser or anything like that.
I can just develop my API in isolation.
Let's go back over to our terminal.
Let's make sure that these tests are running.
It looks like returns the ticket if the ticket is found.
We expected a two hundred but we've got a four or totally makes sense because we've not even defined
that row handler.
And again the other test is passing erroneously because we have not defined that route handler at all.
And it's being captured right now by that not found middleware that we wired up back inside the app
file.
So there's our test file.
Quick pause right here and we'll build out our implementation in just a moment.