So now I want to test this function that's part of my render package, render template, and the very
first thing I notice about this is that this doesn't actually throw an error.
And that's a bit of a problem.
I mean, it does generate an error down here, which is, you know, when I when I say buff right to
the at the response rate, it generates an error, but I don't return one.
So the very first thing I'm going to do is modify this to make it easier to test.
I'm going to have a return on error.
And if there is no error, it will return nil.
So down here at the very end, if everything worked, I can say return nil.
And here, where it generates an error, I'm going to return an error in the same way.
If this is not OK, if I can't get the template from the cache, I'll return
errors dot new can't get template from cache.
Is there anywhere else I'm throwing an error?
No.
So now this throws an error.
Now does this break our handlers?
Let's go to our handlers and find out when I generate a page calling render template.
All I'm getting is a warning that there's an unhandled error and I can live with that for now.
OK, so now I have something I can actually look for.
I can see if there's an error or not.
So let's go back to our render underscore test.
Go and think about how we're going to write our test render template function.
Well, there's one thing I know I know I need to do straight away.
You recall a couple of lectures ago in our render package.
We actually created this variable at the top, which was path to templates.
And when I run my tests in the render package, this is not the correct path.
Fortunately, since I've extracted this to a variable, a package level variable in my render test or
my render test when I create my testing function, which will do right now func test render template,
which is of type testing t I can set that variable path to templates is equal to and I'll make it what
it needs to be for the purposes of this test, which is two levels up from our current directory.
Dot, dot, dot, dot, dot.
And then templates.
All right.
So now we know we have a means of getting to the templates.
So then I need to what what's the first thing I have to do?
Let's we need to have a template cache before we can render anything so we can just say take for template
cache and error are assigned the value of call the create template cache.
So now I have a template cache and potentially an error.
If there's an error, if error is not equal to nil, I don't need to fail.
So I can say dot error and the error is just the error that it was passed.
So that will fail the test.
Now that I've created the template cache, I need to put that into my variable app and that's a package
level variable so I can just assign a template cache equals T.C..
All right.
We're almost ready to call this function.
There's one problem, though.
We actually want to call render template.
So let's look at that for a minute.
Render template is right here.
It wants a response writer, a request the string to the name of the template we want to render and
then our template data and then it throws an error, which we just edit.
Here's the problem.
The response writer.
How do I create a response writer?
Well, if we try to actually call this right now, error equals render template.
We need a response writer.
So let's just call that response rate or I'm calling it two WS for a reason and our request.
And we need to render some templates, say home page dot temple, and it needs to have some template
that which we can just pass in is an empty variable, a pointer to model start template data.
So that should be enough to call it.
The problem is I don't have a response and I don't have a request.
So where am I going to get these things?
While the request is easy, we already have this get session function down here.
That gives me something back.
It gives me an HP request and an error so I can just come up here before I call, render template and
say hour and an error are equal to get session.
If error is not equal to nil, then fail the test error and error, so now I have my error right here.
OK, this has to be a sign there.
Now I have my my my other variable, so I have my request, which is a pointer to an HTP request, but
I still don't have a response rate or I need to have a writer of some sort.
So if we look at the render template package, it says it has to have an HTTP response, right.
Well, is that built into the testing package?
Can I just go?
W-W is assigned the value of each test dot and then we had a new request.
Is there a new response writer?
New response?
No, I don't see one.
Well, there's a number of ways of doing this, but what I'm going to do is actually create an object
using interfaces or create a type using interfaces that satisfies the requirements for a response.
Right.
So I'm going to go back over to setup test.
I'll put them all in here just for the sake of doing it.
So the first thing I want to do is create a type which I'll just call my writer and that's just going
to be a struct.
OK, no members, just an empty struct.
So that's my writer.
And then if we look at the response, let's see HTP response.
Let me find an example where I can just point at it.
So, for example, it should be a response rate.
I'll just point out that when I pointed that it says the type response writer is an interface that has
a header, a right and a right header method.
If we can create a type that satisfies all of those requirements, we can use that in in place of an
HTTP response rate.
So let's go do that.
It's really not that hurt.
So we need to create three things.
So the very first one we created are our type here.
Now we need to create methods on that.
So one was header, for example, func, and I'll call this T.W. just because I have to have a name
for it.
This will be a pointer to my writer and the function has to be called header and it has to return.
And HGP dot header, which we can see if you actually go over again and point at this method, which
is right here, this type, it says it's a header that returns a header.
So we can do that.
That's not hard at all.
So all we have to do to satisfy that interface is create an empty header variable.
VAR H HDP var H HDP dot header and return it.
So there's one method down.
No problem.
Now we need the second one and the second one was a right method, right header method.
So func.
And again this has to have a receiver that links it with my right or type
right header.
And did that take parameters.
Let's take a look at this again.
So right heter takes a parameter of type int and doesn't return anything, well, that's easy.
So we'll go back over here.
So I intend it doesn't return anything and that should be sufficient for that one.
So we have that the last method renderer is right, I believe.
And Wright takes a slice of bytes and returns into an error.
OK, all we can do that.
So the last one is func T.W. Pointer to my writer and it's called Right.
And it takes a slice of bytes, so be slice of byte and it returns an inch and an error.
Now in this case, we actually can't just return a random event.
And this is an area would discover the hard way.
But I will just tell you how to do this.
So we need to figure out the length of the slice of bytes.
So length is assigned the value of Lyndon B..
And I return length and no error take no.
And that should be sufficient.
So now I've created all of the three methods or all of three functions that are required to satisfy
the interface that will allow us to use our own type, my writer, instead of a response rate.
So this W-W is the issue.
So let's make this now what we can actually fix this we can say via WW two WS is of type my return and
then we can just say, well, we've got there right now, we've actually created it so we can actually
just put it right here.
But instead of this, it needs to be a pointer.
It does not implement.
Some methods are missing.
Right.
Byte error.
Let's go back to what do we get wrong here?
Setup test of right there.
All right.
Now, we've created the necessary variable to satisfy the requirements for a response.
Right.
So now we can actually render our template and check for the error.
So we've rendered this template and we've passed it the necessary information.
There are no parentheses after this.
I don't know where those came from.
And we can check for our error.
If error is not equal to nil, we can just say
Teda error, error rating template to the browser there.
And then again, we probably should check for something else.
We can do it right here.
I'm going to do this again, but this time I'm going to call it exactly the same.
But I would call a non existent template something that I know is not in my template cache.
And this time I want to say if the error is equal to nil, in other words, if it successfully renders
a non-existent template, well, it ought not to be doing that.
So I'll say got a template that did not exist or rendered template that does not exist.
All right.
So that should be sufficient, which is clean this up a little bit.
What's that should be sufficient to actually try to run this test?
So let's do that.
Go.
Am I in the right package?
I'm in render go test drive.
Let's see what we got.
And everything passed, so it says here, can't get template from cash.
Where did that come from?
That comes from can't get comtemplate from cash.
That actually comes from a vendor package, I believe, yet right here.
So that is fine.
I don't mind that at all.
That air is just fine.
I can actually get it out of here entirely if I want to and run it again.
And everything passed the way that it should, so now we have tested rendering our template.
There's a few other things we have to try to test as well.
So let's go back to our render package and see what's not tested so far.
So we haven't tested new templates.
Can we do that?
Sure.
That one is absolutely trivial.
We just go back to our render test, create a new test function, which we will call function test new
templates.
And all we have to do is say new templates app.
And that should actually fire that that that function.
And it's not doing anything.
It doesn't return anything.
So this should be sufficient to force it to go through the testing process.
All right.
So we've tested that.
Is there anything else we have to test?
Have we tested adding default data?
Yes, we've tested rendering our templates.
Yes.
We've tested create template cache.
No, I don't see that at all.
So we have to do that.
So let's go look at test template cash or create template cash.
That is right here.
So this takes a map of string template template.
No, it doesn't take anything.
It returns those two things.
All right.
So let's create that test.
That's the last one.
I think we have to do func and we will test create template cache.
And what we have to do is, again, specify our path, the template path to templates.
And that's going to be equal to Doug's templates.
So now we've got the correct value set.
So when this function is called, we will look in the correct location and then we just call it create
template cache.
T.S. error is equal to create template cache.
And we say if error is not equal to nil, then Teda error error and that will fail the test.
Let's try running that now and see if these new tests work.
Cleared the screen run and we have t declared but not used.
OK, so Turkey declared is not used.
OK, well we can't do that then so we don't actually need that turkey variable.
We just needed to run.
Let's try that again.
And everything past perfect, so it looks like we've covered pretty much everything we have to now,
we're not sure yet because we haven't checked our coverage.
We will do that in the next lecture and see if we missed anything that needs to be tested.
That's enough for now.