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
0:02 when all the data is valid makes a lot of sense
0:04 and it works.
0:05 However, one of the important roles
0:08 of testing this is to make sure you're
0:10 doing all the validation in the way that it should be done.
0:13 So in addition to testing this happy path
0:16 when everything's typed in well
0:18 we should write a handful of tests
0:19 for when the name is not there
0:21 when the email's not there, when the password is not there
0:23 when the password is not the correct length
0:26 or all the various things that we care about.
0:28 And I'm going to do just one more test here just to show you
0:32 but of course you should write more than this.
0:35 So we'll say test registration validation
0:38 for existing user. So what we're going to do is
0:41 we're actually going to do basically the same test
0:44 but instead, we're going to come over here and control this.
0:48 I want to say the user exists.
0:50 So let's go over here, and this is going to be a user.
0:53 Now where does a user come from?
0:55 It comes right there.
0:56 Now we could set things like email equals form data.
1:02 I get email, so it's exactly the same
1:04 and things like that.
1:05 But really, the way that it behaves
1:08 it's not actually going to use that property
1:10 but we could pre-configure this
1:11 and maybe I'll make it a little more obvious here.
1:14 Then we can come over here and do a refactor
1:16 create a variable, call this test user.
1:19 Except. Just to be super explicit.
1:22 Now instead of asserting the error as none
1:25 what should the error be?
1:27 It should be it is not none.
1:30 Then the other thing we're going to test for that is existing
1:34 is in that string.
1:37 So there should be an error
1:39 and let's say existing user, something like that.
1:43 Let's go ahead and run this test.
1:45 Oh, what did we get here?
1:48 Something's wrong.
1:49 A user with that email, all right.
1:51 Let's just say already exists.
1:53 We'll search for that.
1:58 Right, so we're looking for already exists.
2:00 Perfect, that test passes.
2:03 And it passes because we're able, again
2:05 to control what that return value is
2:07 and look for a control inside the machine
2:10 inside of what this thing thinks
2:13 the world looks like, right? Perfect.
2:15 So you should go write a whole bunch more tests.
2:17 This is just a class, not a real application
2:20 so I'm not going to go write 100 more tests
2:22 but in a real lab of course this is important
2:24 and you should test it.