Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Testing web apps
Lecture: Testing registration when inputs are invalid
Login or purchase this course to watch this video and the rest of the course contents.
0:00 So test our registration view model when all the data is valid makes a lot of sense and it works. However, one of the important roles
0:09 of testing this is to make sure you're doing all the validation in the way that it should be done. So in addition to testing this happy path
0:17 when everything's typed in well we should write a handful of tests for when the name is not there
0:22 when the email's not there, when the password is not there when the password is not the correct length or all the various things that we care about.
0:29 And I'm going to do just one more test here just to show you but of course you should write more than this. So we'll say test registration validation
0:39 for existing user. So what we're going to do is we're actually going to do basically the same test
0:45 but instead, we're going to come over here and control this. I want to say the user exists. So let's go over here, and this is going to be a user.
0:54 Now where does a user come from? It comes right there. Now we could set things like email equals form data. I get email, so it's exactly the same
1:05 and things like that. But really, the way that it behaves it's not actually going to use that property but we could pre-configure this
1:12 and maybe I'll make it a little more obvious here. Then we can come over here and do a refactor create a variable, call this test user.
1:20 Except. Just to be super explicit. Now instead of asserting the error as none what should the error be? It should be it is not none.
1:31 Then the other thing we're going to test for that is existing is in that string. So there should be an error
1:40 and let's say existing user, something like that. Let's go ahead and run this test. Oh, what did we get here? Something's wrong.
1:50 A user with that email, all right. Let's just say already exists. We'll search for that. Right, so we're looking for already exists.
2:01 Perfect, that test passes. And it passes because we're able, again to control what that return value is and look for a control inside the machine
2:11 inside of what this thing thinks the world looks like, right? Perfect. So you should go write a whole bunch more tests.
2:18 This is just a class, not a real application so I'm not going to go write 100 more tests but in a real lab of course this is important
2:25 and you should test it.