#100DaysOfWeb in Python Transcripts
Chapter: Days 81-84: Unit testing web apps
Lecture: Concept: Testing view methods

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now you've seen testing views in action let's review the core concepts. The idea is, we're going to need to supply a real, legitimate request object
0:10 from the web framework, over to the view method. Probably that view method is already working on the view model which ideally has its own set of tests.
0:17 So these should be pretty straightforward. So here's a straightforward example let's test the homepage. So we're going to do our three As:
0:24 arrange, act, and assert. So we're going to import the index. We're going to make a dummy request. We'll call the index method, passing the request.
0:31 This is the view method the thing the URL generally goes to. What we get back is one of two things: either a response object, or a dictionary.
0:39 Here, this particular one, under this circumstance is going to give us back a dictionary. So we're going to grab the user out of it
0:46 and we're going to assert some things: that they're not none, that they have some bills things like that. Here's the ideal world.
0:51 In practice, it's a little messier as you can see from the size of this box and the size of the fonts. In reality, we're probably talking to a database
1:00 and some other things that make this more complicated. So, again, we're going to import our index. Now, we're going to create a dummy request
1:07 but we're going to also setup some additional information to pass along. We're going to create a mock user here
1:12 and then the big thing is, we're going to patch out the get_user_by_id, to return this mock user. Otherwise, we'd have to have the database
1:20 actually setup, and actually talk to the database. So here we're going to call index. Again, we get back our info, but this operation here
1:28 calls now the fake, the mocked out, get_user_by_id and then we're going to the same asserts that we got back the user, that we got back their bills
1:36 and so on. So in practice, it's not quite as simple as we saw that first time, but not too bad. Once you learn how to use mocked up patch
1:44 you're in good shape.


Talk Python's Mastodon Michael Kennedy's Mastodon