Check out a free preview of the full JavaScript Testing Practices and Principles course:
The "Unit Tests for Express Middleware Solution" Lesson is part of the full, JavaScript Testing Practices and Principles course featured in this preview video. Here's what you'd learn in this lesson:

Kent live codes the solution to the Unit Tests for Express Middleware exercise, where the test checks that all posts are returning from the database, the specific post is being returned, and updatePost updates the correct post with the given changes.

Get Unlimited Access Now

Transcript from the "Unit Tests for Express Middleware Solution" Lesson

>> Speaker 1: Hopefully that was exciting and fun. Let's go ahead and implement, or walk through the solution here. And then we can, if you have any questions about this, then please do ask. So we already had the database set up for you, and then this test to get all the posts that are in the database.

[00:00:24] These are actually unauthenticated requests, so it's a fair amount easier. Some of the authenticated requests have to do a little bit more, and we'll take a look at what that looks like in a little bit. So, we have our post controller, the post controller we want to I call getpost on it.

[00:00:46] That's an asynchronous method so I'll await that and it also accepts a request and response. Here, let's pull that out just so we can see that for sure, Oops. And so getpost and request response. It takes the [INAUDIBLE] and calls JSON on it. So, we'll make the request equal an empty object, and we'll make the response equal an object that has a JSON just function.

>> Speaker 1: And then, we wait for the getPost to resolve. And then, we can make our assertions we'll expect Json to have and call times once, and expect res jason, we could, yeah, to have been called with an array of posts. And these need to be the posts that are in the database.

[00:01:46] So let's go ahead and retrieve those from the database say allPost await db.getpost and actualy we'll just say this is an object that has a post property. Okay, great it's always kind of freaks me out when I make a bunch of changes and it says they're still passing and like wait.

[00:02:08] So you'll want to not make sure that. And actually if you like it's this gigantic amount of information that's generated every time you hit the database, kind of cool. Okay so that is working and that's pretty much everything that you need for that functionality for you to be confident enough in functionality.

[00:02:36] Okay so the integration test that we gonna write in a little bit are a little more involved and you will need some more time for the exercise. So, given the time constraints, that´s actually why I only had to do this one test, so that we can move on to the integration portion.

[00:02:57] So I´m gonna show you what these tests look like and then, I´ll actually copy over a couple Test. And we'll give a look at those and talk about test object factories. So actually I'll do the users other than the post. So when you get the all the users test, I'll just do these.

>> Speaker 1: And paste those in there.
>> Speaker 1: Okay, and this one is identical to that one we had before. Okay so, we're gonna need a couple extra things.
>> Speaker 1: Yeah.
>> Speaker 1: And this request and response I'm gonna be coming from some other things.
>> Speaker 1: And here why don't we for illustration purposes we'll in line all of our properties here and request params, put this up here, sorry, in retrospective you to go if I had this all set up ahead of time, so I apologize for that.

[00:04:15] Direct our buyers going to be right here. Okay, let me put this right there.