Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Testing web apps
Lecture: Concept: Testing view models

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's review testing view models. They're the simplest of the web framework integrated pieces that we have to test. So, we saw that we need to provide
0:10 some sort of request data, set up the flash request and other flash features for the view model. But other than that, it's just calling
0:18 the functions of the view model. So here we're going to test that if we try to register with no email then we're
0:25 going to get an error saying, you have to have an email. So, how do we do it? We have the three A's of testing.
0:31 We're going to arrange, which means we're going to import our registration view model and set up the form data. Notice here, there's no email.
0:38 It's just an empty string. And then, in order for us to work with that data we have to pass it through this test request context in a context
0:48 manager using a with block. So here we say the path is /account/register and the form data that's submitted is email empty password the letter a.
0:57 Second thing is to act. We're going to validate that form data which is going to look and say, oh there's no email
1:03 you're going to have to have an email. It's going to set an error saying that's the case. Then we're going to assert at this error set
1:09 and that email is in the error. Right? You don't want to be too specific. Just specific enough to verify like that case is happening.
1:17 But not so much so that if you slightly change the wording then you've got to rewrite your test. All right, so here's a basic way to do the test.
1:24 Why are we not using mocking and mocking out the database call? Because this validation is more simple. Right? We never made it down to the database
1:32 validation layer, so we don't need to.


Talk Python's Mastodon Michael Kennedy's Mastodon