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.
0:02
They're the simplest of the web
0:04
framework integrated pieces that we have to test.
0:07
So, we saw that we need to provide
0:09
some sort of request data, set up the flash request
0:13
and other flash features for the view model.
0:16
But other than that, it's just calling
0:17
the functions of the view model.
0:19
So here we're going to test that if we
0:21
try to register with no email then we're
0:24
going to get an error saying, you have to have an email.
0:27
So, how do we do it?
0:28
We have the three A's of testing.
0:30
We're going to arrange, which means we're
0:32
going to import our registration view model
0:33
and set up the form data.
0:35
Notice here, there's no email.
0:37
It's just an empty string.
0:39
And then, in order for us to work with
0:42
that data we have to pass it through
0:44
this test request context in a context
0:47
manager using a with block.
0:49
So here we say the path is /account/register
0:51
and the form data that's submitted
0:53
is email empty password the letter a.
0:56
Second thing is to act.
0:57
We're going to validate that form data
0:59
which is going to look and say, oh there's no email
1:02
you're going to have to have an email.
1:03
It's going to set an error saying that's the case.
1:06
Then we're going to assert at this error set
1:08
and that email is in the error. Right?
1:11
You don't want to be too specific.
1:12
Just specific enough to verify
1:14
like that case is happening.
1:16
But not so much so that if you slightly
1:18
change the wording then you've got
1:19
to rewrite your test.
1:20
All right, so here's a basic way
1:22
to do the test.
1:23
Why are we not using mocking and mocking out
1:26
the database call?
1:27
Because this validation is more simple. Right?
1:30
We never made it down to the database
1:31
validation layer, so we don't need to.