#100DaysOfWeb in Python Transcripts
Chapter: Days 81-84: Unit testing web apps
Lecture: View unit tests: index page
0:00 We've tested the view models, now let's test the views. And this gets a little bit simpler because we're using the view model.
0:07 So if you look here, there's less going on in each one of these things. There's still stuff happening.
0:13 For example, in this one we're still calling add_payment. We're still doing a 404 when there's no bill, for example.
0:20 We would like to check those things. But there's actually a lot of validation that's already been done on those steps, right? So that's really good.
0:28 Still, let's go ahead and copy what we've done here a little bit. It's not going to be exactly the same but it's going to be similar.
0:35 Come over here and let's import unittest stuff. We're going to need to import unittest.mock. We're going to import testing.
0:43 Okay, so we're getting close. Now let's say test index, not view model, just like so. Well, this request is going to be similar.
0:50 We're going to create a dummy request and let's first just avoid this for a minute. And we're going to come over here
0:57 and this arrangement from billtracker.views. default import home. So let's go, we're going to call this home
1:06 method, and let's take that out for a minute. And just say our goal is to call home and we have to pass a request to this method.
1:14 And we're going to get some kind of info dictionary back. And let's just test that what we get back is going to be a user.
1:21 So we're going to get back a None user from here. Remember, when you hit that page that's one of the things the view model gives us back.
1:28 So, theoretically this will work, maybe. Let's go here and say we're going to run this. Now it turns out, mmm, not so much.
1:36 Again, trying to interact with the database. So we got to put our patch back in and we'll move this down here. And we need our test user again.
1:46 'Cause if we're going to patch out this method it's got to return something which means we're going to put these, also, back at the top.
1:53 Now we still have it, if that's not the full name right there that freaks out. Okay, now let's try it once again. Tada!
2:01 Everything worked. So we're able to call this method here. If I go to definition you'll see it's the route one right here.
2:08 Now of course it worked because not much else was happening. We've already tested that line so the line below it, pretty likely going to work, right?
2:15 Let's go and write one more of these tests for the details. Instead of having you watch me type it in let me just start from an existing one.
2:24 Okay, so if we're going to call the details view we have to pass along something in the match dictionary. This is going to be the URL
2:32 or where the parsing of the URL goes where we're supposed to pass the bill_id, right? And we still have to return a user
2:37 user has to have some bills, and so on. So we want to get details, get here. And let's call this. Okay, so run it one more time.
2:46 Oh, something's not quite right. An object is not callable. Well, that's because there's one more layer of validation here. Let's move this up.
2:56 All right, so we'll say patch1 is going to be this. Equal sign. Patch2 will be that. And this is going to be get_bill.
3:05 id on this will be bills. No zeroes. You get back the first bill there and we'll say with p1, p2. All right, let's try to run it.
3:14 I think there's one more thing we need to do. Oh, yes. So we got a response object, not a dictionary. What's going on?
3:21 Well it turns out that this thing returned a 404 response, rather than a dictionary. And that's because our bills do not belong to this user.
3:30 And so it's saying you don't get access to that bill, remember that? So we need to say bill.user_id == user_id. All right, let's try it one more time.
3:39 There we go, tada. Our details have passed. So we're testing our views and we could actually also go and test the post. Create a bill.
3:47 Make sure that the value that's still owed on the bill is less after we call the post with some certain amount. Things like that, all right?
3:55 We'd have to mock out the add_payment and capture that information, maybe, but we could do it. All right, so that's how we test these view methods.
4:02 We're going to stop at just these two but of course there's lots of views to write in our web app and lots of 'em need testing.
4:08 Hopefully you saw, there's not a whole lot left to test here because so much of the testing was already done in the view models.