So how did you make out with the exercise, writing tests for a reform package?
Hopefully you figured it out and hopefully you found the the errors, the couple of errors that I had
hidden in there.
If not, don't worry.
We'll figure that out right now.
So right now, we have two tests.
We have one for the valid function test form valid and one for the required function.
And those, of course, correspond to these the valid and the required.
So those both work.
I can open up a command prompt and make sure I'm in the right directory and I am and just right go test
Dashti and they both pass.
Yes, they do.
So let's write one for the next function and the next function is has and that's a really short function
that should be easy to test.
So let's write a function for a test for it.
Func test form has and in there we need to create a request and we need to create a post form, a form
with that request, which we did right up here in test form required.
So we can do the same thing here and then we need to actually call and test that function so we can
just say has which will be Boolean is assign the value of form it has and then it takes two parameters,
the field, which can be anything.
So whatever one that I know doesn't exist is what I'm going to try right now and we need to pass it
our request.
So when this is called, we're calling it with a request that we made here and then we're posting adding
to the form the post form values from that request, which are currently empty.
And just so you know, we didn't have to create this request necessarily.
Well, we did right now, because we're passing a request to it.
We are.
But that's one of the problems.
Here's one of our hidden errors.
Let's see how this works.
First of all, it's going to seem like it works.
So I call this I'm passing nothing in the post.
So this field should not exist.
Therefore, this should return false because it doesn't have it.
So I just say, if has in other words, if it returns true, then write it er er and the, the message
will be form shows has a field when it does not.
And so far this seems OK, so we'll go run our tests and it will pass.
And it does, and that's correct.
So now let's create another request and check the situation where we know we have posted values in there.
So we already have a request.
I don't need to recreate it, but what I can do is say set up some values.
So I'll say I posted data, which will be a variable.
I create posted data is assigned the value of an empty world values.
So that's the type.
So creates a variable of type YORO values, which is what post form is, and it has nothing in it right
now.
So let's put something in it.
Posted data, don't add and we'll put a key of A and a value of it.
Doesn't matter what it is, as long as I know it's in there, then we need to reinitialize our form
with our new request so we can just say form equals new.
And I could again put a sign this value to the request or as I did, as I did in previous examples up
here, for example.
But I can also just pass in posted posted data itself because that's of the correct type.
So the only reason I created a request here was because when I call it, I have to pass in a request.
Now watch what happens.
I've now added the A parameter with the value of a two hour posted data.
So when I call, here's our second test in this test has is equal to form DOT has and I'll say a and
the request.
What I think I'm doing is passing it the correct information.
Check this request for the existence of a field named A in the post request.
So I should be able to say if it doesn't have it, it has its faults, then write an error together.
We can say shows form does not have field when it should.
Now so far this seems to make perfect sense.
So let's go back and run our test.
Go test Dashty and it fails.
It says shows form does not have field when it should.
So that test we just wrote seems to fail and that appears to make no sense whatsoever because I took
this, I created my my post data, I put the value in there.
It's named a I reinitialize my form variable and past it the post of data.
So this should pass.
So let's go look at the the the function and see what the problem is.
And the problem is right here, when I actually check it's this line in line 42 in my code, I'm saying
go to the request that you passed me.
Look at the form on that request and check for the existence of the field named whatever this is, whatever
the first value is.
So when we call this, I'm passing in my test, I'm passing it a here and I'm passing that request we
manually created right in our test.
That's the request I'm checking.
What we should be checking is this one in the receiver.
That's why we have this line right here.
Form equals new posted data.
When I call form, I'm actually looking at the request associated with the form.
And this is not only unnecessary, we're not doing anything with it.
And in fact, we can and will eventually get rid of it.
So to make this test work properly, what I need to do is fix this line and not check on our short form
instead.
Instead, I should be checking f the value from our receiver.
Check that.
And now this isn't even being used.
I'm not going to delete it right now, but I will a little bit later on when we actually run the application
because now it's totally unnecessary request.
So I can go back now, now that I fixed this line, go back to my command prompt and run the test again.
And it passes, and that is one of the hidden errors.
So that's the test for his function.
What's our next function?
Let's go back here and have a look.
The next one is min length.
So let's check that one will create a test for min length func test form min length.
And what do we want to do here?
Well, if we look at the function again, min length appears to take this first parameter, which is
our field, then we're checking.
The second one is an ant and the third one is the request.
And as you can see, here's our second error right away.
I know one because I just fixed it up here that this is checking the wrong parameter is checking the
are the request from here when it should be checking the request that's actually associated with our
receiver.
So I'll just copy this.
And replace this and again, that means this parameter is no longer required and there's no point in
having parameters and functions that we never, ever use.
So we'll get rid of that in a bit.
First of all, let's get the test passing so we can use exactly the same logic.
And now that I know I don't need a request.
Well, actually, at the moment I do.
So I'll create a request here, but we'll get rid of that momentarily.
OK, we're just building a request we're not even going to use.
I can create a new form using this request we just built and we will write our first test.
And our first test is we'll make sure that min length doesn't work for a nonexistent field.
OK.
So in other words, I'm going to just do a really simple thing form mind length.
I'll call that and I'll call it with a field I know does not exist.
And no fields exist in the post data right now and give it some length and then the request and will
write the test if form valid.
In other words, this should be set to fault's right now.
I know that the form, the form valid should return false because it's checking the length of a non-existent
field.
Then I need to throw an error.
Teda error shows a min length, let's say form shows, form shows in length for non existent field.
OK, so let's run that test, see what happens.
And it passed, so this time will create a post values again, just like we did above posted values
is assigned an empty URL values, and then we'll add a feel to it.
So we'll say posted posted values, dot ad and we'll call our field some field and we'll give it a value
of some value.
So that is one, two, three, four, five, six, seven, eight, nine, 10 characters long.
OK, so now we can read our test so we can just easily say form equals new
and it'll be posted values.
So that creates a new form and it passes it this information for the for the form values.
So it passes in one parameter with the name of some field and the value of some value.
And now we have a form that has some data we can test.
So let's test it and we'll say form dot min length and the field name is Summer Field.
The length will put it 100.
So we know that it's going to not going to be long enough and we'll pass in that request that we're
not doing anything with which we'll have to get rid of eventually.
And we can just say, if formed valid, if this is if it shows the form is valid, that fails the test
because we know that some value is less than one hundred characters long.
So we'll say to air shows, min length of one hundred met when data is shorter.
All right, let's test that go test dash.
And it passes, so now we need to test the other case, create posted values, posted values we're not
going to create.
We're going to reinitialize posted.
Values is equal to your values.
All right.
So that reinitialize are posted values to be empty and then we'll add a value to it.
Posted values don't add and we'll call this one another field and we'll give it the length of ABC.
One, two, three, something greater than one.
We'll just test for a minute length of one.
Now we have to reinitialize our form.
Form equals new and we'll pass in our posted values.
And now we call them in length form.
In length.
And our field is another field arm in length will be one.
So we know what we're passing in is longer than one character and we'll pass at that request, which
we're not actually using at the moment.
But we'll take care of that shortly.
And now this should pass validation.
So we we need to test that.
If it doesn't pass validation, if not form valid and fail the test teda error shows min length of one
is not met when it is.
All right, let's go back and run our tests.
So go test Dashti.
And everything passes perfect.
All right, so, so far, so good.
Do we have anything left to test is email.
That's our last one.
Well, that one should be pretty easy to let's write that test.
So we'll write a new function.
Funk test form is email.
And we need to as we generate along, we'll create a formal request and our form
and we will look at these email functions.
So what does that require?
That requires just a field name.
That's easy enough.
He formed out is email with a nonexistent field.
X doesn't exist.
And then we just check to see if the form is valid.
If formed valid t don't.
It can't be valid because we passed it no value whatsoever and the error should just say something like
form shows valid email for non existent field.
OK, so let's run that test.
Go test Dashti, perfect.
So now let's try passing at a valid email, so we'll just again, as we've done right along, posted
values will create that variable is a sign the value of an empty.
You are Elgort values and then we'll add a value to it.
The posted values dot ad and we'll call it email and we'll give it a valid email address.
So a valid email address could be something as simple as me here.
Dot com.
OK, and we need to reinitialize our form.
So form equals new and we'll just put in our post-it values and then finally we'll call it the test
together form DOT is email and the value of where we want to check is email.
And that's all we have to do.
And then we check to see if we get it.
Now, we should be getting a valid form now, right, because we passed it a field named email with
something that's in the form of an email address.
So we need to check if the form is not valid, if not form valid t dot error.
And the error would be.
Got an invalid email when we should not have.
All right, let's run that test for the screen.
Run the test and it passes.
Perfect.
All right.
Anything else we should be testing here?
We should be testing, passing an invalid email address.
And what we can do is just copy this entire section pasted here and modify it.
So posted values is now equal to your values and posts and values will add an email that will just put
X in there, which we know is not an email address.
And then we reinitialize our form.
We check it for the field named email, which currently has the value of X, and if the form is valid
this time, that got valid for invalid email address and I can just delete the rest of this.
And that should work.
So let's run our tests.
And everything passed good.
Now there's a couple of things we can do here that actually will make this a little bit cleaner.
So we've passed all of our tests and we'll check our coverage in a minute.
But right away, we don't need this request.
This this creating this request is utterly useless.
All we need to do is to create a universal values.
So what I'm going to do is copy this and replace this line with that and then change this to an equals,
not in the sign.
And up here, I'll just pass in posted values, because really, when you think about it, this new
function requires you are values.
That's what it requires.
It doesn't require a request.
So we were creating request for no point whatsoever.
Now, up here, when we thought things were working properly with the highs, we were creating a request
as well.
So what we were doing that because we thought we had to pass request as a parameter.
So let's fix that right now.
Let's get rid of the request for the has this parameter in the house function.
So that is gone and we'll go back to forms test.
We don't need this request anymore and we don't need this request anymore.
And now we should be able to run those tests and still have them pass.
Go test, so it'll all work just fine.
Great in the same way, when we call min length, we don't need that requestor anymore, so let's get
rid of that and get rid of it right here.
And I think that's it for that one.
So we can go back here and remove this parameter, which is pointless and serves no purpose whatsoever
and see if we have everything working up.
One more down here, get rid of that one.
So we should be able to pass those tests again.
And we do.
That's great.
OK, now the last thing I need to check before I can actually say that this part is done is to make
sure now that I've changed that function, does our program still compile?
So I'll go back to my terminal window, clear the screen, I shall open a new tab just so I don't have
to find this folder again and go to my Gulland projects and my bookings and I will run this.
So remember, running this now isn't as easy as going go run command
web Stargirl.
That'll fail because there's test files in there.
So we actually have to specify the three, the three files that are in there that are not test files
may not go and command web rootstock.
Go and command Web middleware Dudko and hopefully it'll run and it fails and it fails at handlers go
line Eighty-seven.
And that's why I'm checking this.
So let's go fix that right now.
Handlers go line eighty seven right here, so let's get rid of that.
We no longer want the request in there and now we'll try running the program again.
So I'll just hit the up arrow to get the command back.
And it runs all right, so now I'm going to go back to my other terminal window, which is in the correct
directory.
Let's check our coverage on this and see if we have everything we want covered in the forms package,
go test, dash cover.
And it says eighty one point eight percent.
I thought I tested everything.
Well, let's find out what we didn't test.
So I'm going to use my shorthand coverage, my little alias, and you might have to type the command
all the way out there.
Let's see what we have here.
So right.
Inform start go.
At no point did I try the get function.
Well, I'm going to have to do that.
That should be pretty easy.
But the other one has 100 percent coverage, which is perfect.
All right.
So how do I check the jet function?
Well, it's really easy.
It's what I'm going to do is close this and go back to my tests under form tests.
And right here, when I say, for example, I just need to check it in one spot and then I know that
it works.
So we need to find somewhere where I can call the jet function and get no error.
And then I need to find somewhere where I can call the jet function and get an error.
So how am I going to do this right away if this one if it shows the form is valid, if this test fails?
Well, after that, I know that there should be an error in there.
So I can just say and I'll just store the values.
So I'll call it X.
No, I'll call it is error is assign the value of form errors get and then the field name I used up
here, which is X, and then I just need to make sure that there is no or that there is an error.
So I'll say if X is equal to nothing because in the jet function, which is right over in our errors
file, I believe right here in the jet function, it returns an empty string if there is no error,
otherwise it returns the error.
And I need to check both of these conditions.
So when the first case where I'm expecting an error to be there, so if these are not yet these air
X if is error is an empty string, that didn't work.
So I need to throw an error, pilot error and I should just say something like should have an error
but did not get one.
OK, and now I need to use the same logic.
So I'll just copy this when there is no error.
OK, so when do I know there is no error if the form is valid.
Is this the case now?
I'm not going to say so.
It shows mean like the 101 data is shorter, so I need to check when there is no error and oh here we
are.
So if the form is not valid.
So after this test, when I'm passing it another field and checking for a length of one, I know this
test should pass.
Therefore there should be no error.
So I can just paste what I had there, change this to an equals because I can't reinitialize or redeclared
the variable twice in the same block.
And I'm going to look for the field not called X, but the field called another field.
So and now I say if error is equal to nothing, if error is not equal to nothing, should not have an
error but got one
and that should do it.
So let's just look at this again.
In the first case up here, I'm calling with an empty post for him.
There's nothing in there whatsoever.
So I know there should be an error.
And I check to say if there's no error for the field X, then write an error.
Fail the test down here when I know I'm calling a valid request and my form should be valid.
This what I call form errors.
Get on another field, which is the field I just tested right here, then I should have an error.
So if so, I should not have an error.
So I say if error is not equal to an empty string, then I write, fail my test and write the appropriate
error message.
So let's test this, go back to our web browser and run our test.
Everything passed.
Now let's check our coverage.
I have 100 percent coverage for errors Torgau and I have 100 percent coverage for Forms Dutko, and
that is exactly what I wanted.
All right.
That is the last bit we need to do for this particular package.
So let's move on.