Check out a free preview of the full JavaScript Testing Practices and Principles course:
The "More Mocking with Users" 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 reviews an example of how to construct a test that ensures the users being retrieved from a database are what is expected.

Get Unlimited Access Now

Transcript from the "More Mocking with Users" Lesson

[00:00:00]
>> Kent Dodds: Okay, so with that we can make a more specific assertion. Yeah, I'll just copy over the code and then we can talk about it. Okay, now, we're looking.
>> Kent Dodds: [SOUND] And actually let's pull this thing into ms.
>> Kent Dodds: Okay, man.
>> Kent Dodds: [SOUND] Okay, pull this back, get this back.

[00:00:33]
>> Kent Dodds: Okay, there we go. And we'll also need our database.
>> Kent Dodds: Database, actually let's put all that stuff in.
>> Kent Dodds: Okay, cool. Now, let me explain what's going on, on all the stuff we did to make this happen. Okay, so we're still just creating these fake versions of requests and response objects.

[00:01:06] We're still validating as being called once. And then we're getting the first call, the first argument of the first call. So in our case, so this is the first and only call and this is the first argument. So we're getting that users object. We're pointing users off of that object and then we're verifying that the length is greater than zero.

[00:01:26] So we don't really care how big it is just that there are users in there and we're getting some users back. And then we're gonna go through, and that's just like, before my I do my next assertion, let's just make sure things are set. We don't even need that assertion necessarily because we're kinda covered with this one.

[00:01:45] So yeah, you can have an insertion like that or not. It's kind of, there's no real science behind that, that piece. But yeah, that will just signal a problem earlier on that, this one is going to fail because there aren't any users at all anyway. So anyway, here we're gonna actually get the users that are actually in the database, and compare those to the users that we got from this API, or from this end point, this method that's supposed to give us the users that are in the database.

[00:02:19] And so I created this little function safeUser that just removes salt and the hash Just to make this a little bit cleaner. So the users we get back should be the same as the actual users minus their salt and hash.