In this video we're going to work on our first test right here.
So we need to somehow make a request or a POST request specifically to API slash tickets.
We're just gonna make sure that we get back a status code that is not a four a four.
Why would we not want to form a four.
Well if we open up our apt to yes file you might recall that inside of here when we copy pasted all
that code over from our service we had previously put together this root handler right here.
This catch all root handler.
So if we ever got a request coming into our application on any method to any routes that we did not
somehow recognize we were going to immediately throw if not found error that results in a 4 0 4 status
code being sent back to whoever made the request.
So if we could somehow make a request or a POST request to API slash tickets and then inspect the response
code or the status code and make sure it's not a for a for now I'd be a pretty good way of implementing
the test right here.
So let's give that a shot.
I'm gonna write out response is a wait we'll make a request to our app I want to make specifically a
POST request to API slash tickets.
I'm going to send along an empty object so eventually we're going to require that we send along a title
and a price and so on.
But for right now we can just send some empty request because all we really care about is that we don't
get a four a four if we get back a status code of say four hundred which is an invalid request.
That's fine.
I just want to make sure that well we get something besides a forum for.
Not to somehow get the status code back and make an assertion about it.
We can take a look at this response right here.
So the response is going to have a status code property assigned to it and that's gonna be the status
code of the response we got sent back.
So we can write out an expectation and look at that status I'll put it in response dot status.
Now I'm going to expect that to not to equal or for I'll save this I'm going to go back over to my terminal
and let's see how we're doing.
So it looks like that test is failing.
That's good.
That means that right now you're getting a four a four.
So now we can start to create that root handler and as soon as we define it and send back some other
kind of response like maybe a four hundred to two hundred whatever else then that test should pass.
So let's give that a shot.
I'm going to find my roots directory inside of roots.
I'm going to make a new file called new dot t s so this new file is gonna responsible for creating a
root handler to create a new ticket.
To create this right handler at the very top we're gonna add in a couple of imports.
So I'm going to import express I'm going to import the request and response types from Express
I'll then create a router using express stop router on the router I'll tell it to expect a post request
to API slash tickets and I'll put in my actual route handling function I'll put in my arguments of req
and I will annotate that as type request and rez an entity that has type response then inside of here
we get put together for right now just some implementation that will make this test pass so all we really
care about right now is sending back something that is not a status code for a 4.
It's all simply do a red dot send status which is how we send back just a status code by itself of about
200
then at the very bottom I'm going to export the router and remember and all those other rep files we
had done back in cyber auth service we had it renamed this router variable because we're going to eventually
have many different routers created and in use inside of our abductees file.
So I'm going to rename the thing on the fly and call it as rate ticket router.
The reason I'm not calling it new ticket router is that someone might kind of misread this and think
that it's new ticket router as in trying to create an object out of ticket router to simply call it
create instead
going to save the file I'm going to go back over to my apt yes file remember after creating the router
we have to import it into this file and then wired up to our Express application.
So at the very top I'm going to import
great ticket router from roots new
then right after our cookie session middleware I will wire this thing up so app use and we're gonna
tell it to use it to get router
let's save this to go back or my terminal and now in theory our tests should be passing.
Yep looks like it is definitely good step forward.
Let's take a pause right here we come back the next video we'll move on to our second third fourth and
so on tests.