#100DaysOfWeb in Python Transcripts
Chapter: Days 81-84: Unit testing web apps
Lecture: Concept: View model unit tests
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's review this concept of testing view models. The idea is we have to provide a little bit of infrastructure to them.
0:07
They typically take a request object and they often work with databases or other services. So, here's what this generally looks like.
0:15
Now, let's supposed we would like to test that a user's trying to register for our site but they're not providing an email.
0:22
That means we want to give them an error saying "Hey you have to give us an email." rather than letting them register with no email.
0:29
So how would that looks with view models? Well, we want to do this test, so we're going to arrange. Remember the three A's, arrange, act, and assert.
0:35
We're going to create a testing request. We're going to set the post dictionary to what the form fields would be. There's an email they didn't enter
0:44
and a password which was very secure it's pw, they did enter that. We'll go and do the action create the view model and validate it from the form.
0:52
Then we want to assert that in the error message first there is an error message and that in it, there's an email string.
0:58
So, something about the email is wrong here. So this let's us do a very very simple test of the validation being performed
1:05
by the view model for logging in. Now, in reality, we saw that it's probably talking to the database.
1:10
Maybe here it's not because there is no email at all. But in reality, we also have to use the patch object to avoid talking to the database
1:17
if that's a goal we have.