Hey everybody has gone this Caleb with their slopestyle.
And in this video we're going to continue unit testing.
But this time we're going to be actually testing the server basically that our images are housed on.
That's not our API it's not our server.
But the cool thing is that we're going to be able to create a mock of you r l session and basically
a mock is going to allow us to create an imitation our own version of your Else session but we'll be
able to actually create it and slim it down basically to the most primary components that we actually
need to test.
And it just gives us access to it in a much easier way than needing to use the entire You are all session.
So pull up your project and what we're going to do is we're actually going to create another file in
the helper's folder called download client.
So go ahead and right click click new file and then it's just going to be a basic swift file called
download client.
OK.
Now create a class and name it download client Calixto.
And what we're going to do is we are basically going to put one function and it's going to look familiar.
It's going to basically be this same function download image with your URL so phunk download image with
you r l u r l of type string and we're going to go ahead and basically set our your all to instead of
just being a string we can change it to.
And actually r l by typing guard let u r l equals u r l from string and we can pass in that r l string.
Else we're going to go ahead and just call fatal error.
And now we have a u r l of type u r l and now we can do is we can basically perform our day to task
where we pass in the U R L and we get no data response and an error at the very end.
Just like you are l session.
But the thing is we don't have a session we don't have a u r l session but we need to make a mock u
r l session and we're actually going to go ahead and create a variable called session of type u r l
session and it's going to be equal to u r l session shared.
Now inside our function we can call session data task and we can pass in our u r l here and we can push
enter on our completion handler there and type in everything we need like data response and error.
And then at the end we'll just go ahead and erase the code in there for now.
And of course at the end of a day a task you need to resume's so that it actually completes and it knows
when it's done.
So basically we have a function that we pass a u r l into.
We create an actual u l instance we create a data task from our session here.
And then you know we call resume at the end so that it actually you know does what it's supposed to
do and here will be code to create image.
So if we try to build this you'll see that everything works.
It builds successfully.
The code is good but basically what we need to do is we need to test this.
And so we're going to go ahead and right click click new file and create a unit test case class called
download client.
Client tests go ahead and save that.
And then what we're going to do is we're going to get rid of all the beginner boilerplate stuff.
We're going to import our project here by typing testable import photo load just like we have been.
And then what we're going to do is we're going to go ahead and basically create an instance of our download
client by typing var such as of type download client and then we're going to go ahead and set it up
in set up.
So go ahead and type the system under test is equal to download client and instantiate it.
Then when we tear down we're going to say sut equals nil like so.
So very cool we now have our download client setup in our test or in our test case.
But what we need to do is we need to write a test and we're going to actually just as an example I'm
going to show you how you should test your API to make sure that you know you're using the expected
host and using the expected paths for particular endpoints.
It's really really helpful.
And you can actually do this even before the API is done.
If you have an idea of what the API will be like you can do this before it's even up because we're actually
just using a mock or else session.
So let's go ahead and write this test funk.
Funk.
Test download.
And we're testing to make sure that it uses the expected host.
OK.
Now what we are going to do is we're basically going to take our image you are l.
We're going to call our download image function from our download client and we're going to go ahead
and basically pull out your old components and then verify that the moral component for host is equal
to images dot pixels dot com because in our helper's you'll see that is the host from our images we're
going to make sure that it's actually using the right host.
So go ahead and type let image you r l that's going to be equal to image or L strings.
And we're just going to pull out the item at the third index because that's the image we've been using.
Then we're going to call the set system under test download image with you are l we can pass in our
image you are all there.
And now we can actually create an instance of your elso guard let your L equals you r l from string
K like so and we're going to pass in the image you are l.
Otherwise we're going to call ex-city fail since we're in a test and then return.
OK we've done that before.
Next we're going to go ahead and create the components from this u r l.
So Type let your L components equals u r l components and we can basically use you r l k.
And we want to resolve against the base you are l.
That's true.
So what we're going to be testing for is equivalency so X-C assert equal and we're going to check your
components.
Host K because we're checking to make sure that the host is equal to what it's supposed to be.
And if you remember it should be images dot pixels dot com.
So if I run this this test should pass.
But the interesting thing is that we have not actually properly set up a mock your L session.
And so we're going to do that now.
And so we're going to extend our our download client tests and we're going to basically create a new
class called mock you are all session.
So go ahead and type extension download client tests.
OK.
And we're going to go ahead and create a new class called mock session and it's going to basically have
a couple of properties you know one that we need the most is yourself so type var you are all that is
of course of type you are l and it's optional because it's not you know given a value until it's initialized.
And so then what we're going to do is we're going to go ahead and recreate the function data task with
you r l so type phunk data task with you r l of type u r l and then we have a completion handler which
is escaping and of course we have data.
We have a moral response.
And we have what is it an error I think.
And then of course the return there is void and the function itself data task returns a your L session
data task can't.
So there's that.
And then what we're going to do is when this is called we're actually going to set self your L K for
the session.
We're going to set that to be equal to whatever you are l we pass in when we call data task.
OK so then the session then has a u r l initialized.
And then what we're going to do is return your L session shared data task whoops data task with you
Earl.
And we can pass and the u r l from r u r l session.
That's how that's going to work.
So if we build this it's going to go ahead and go away the errors.
And now we have what is called a mock u r l session.
This is basically your L session but with the function that we're actually testing and the only property
that we really care about.
So that's what's cool is we can basically create a smaller much much easier to test version of your
session to make sure that it's working the way that we want it to work.
Now this might seem silly but basically we're testing to make sure your L session works.
You know but Apple built your L session so we're basically going to create our own to make sure that
it's working the way we want in our particular use case.
So there are a few things that we still need to do what we need to actually do is to make sure that
when we use our mock you are all session that it's actually able to access this function from your session
and the way we can do that is with a protocol so we can go ahead and type protocol session protocol.
That's what we're going to name it.
And the function we're just going to basically take the function that we wrote here.
I'm going to copy it and then paste it in K because that is the function we are passing and you are
l.
We have our completion handler and that is the protocol that we need.
And if we conform to this protocol our mock you are l session should be able to conform to it as well.
So go ahead and create an extension of your Else session.
OK you Arel session and we're going to conform to the session protocol.
OK very very cool.
Now since this function is already a part of your session we don't need to actually type it in the extension.
We can just go ahead and leave it as it is but where it definitely becomes important is in our mock
you or all session.
So go into your download client tests and what we're going to do is in order for mock your session to
successfully utilize this data task function we're going to go ahead and conform to session protocol.
And now we have access to this basically we've conformed to the protocol because we have added in data
a task and you know what.
If I were to actually get rid of this it would tell me that I'm not conforming to the session protocol
in just a second it will get there.
It'll tell me hey there we go OK.
Undeclared use of type session protocol.
And if I go down here I can call a to task and there it is there is our function and we can paste our
code back in here self-doubt you are l equals u r l and then return or session Scott shared dot data
task with you l and pass in the L U r l.
Cool.
So we are now conforming to that mock are Session now is actually accessing the proper function from
your session which is very cool.
And basically we can now use this as the actual session that download client uses.
So in download client go ahead and set up the session here not to be your whole session but instead
to be Mach. you are all session.
But what happens when I try to build that it's going to say hey you don't have access to that here.
So what I can do instead is set this to basically conform to session protocol because it's going to
be able to access either our mock you or all session or an ordinary session.
But since we're using it with a mock your all session it's going to use that.
So very very cool.
That is now setup we can now go back and modify this to fit our needs a little bit better.
So in our tests we're going to go ahead and set our client here to have a mock session and so we need
to go ahead and create one mock you or else session of type mock your session.
Then we need to set it up and initialize it.
Mock your all session equals mock all session and then check it out.
If I type s u t dot session I can set it to be our mock u r l session then we can use our own little
mocke and this is really helpful for things like for libraries that are just really really big that
have a lot of different functions and features and abilities.
You can basically strip it down and test only the features and functions that you really need.
And so we now have set our download client to have a session of our own maake which is perfect and you
can basically mockup anything you want to make sure that it works the way that you're hoping.
And now if we go ahead and run this it's going to use our mock or else session that's conforming to
the session protocol that we built and it'll test it.
It'll run and it looks like our test successfully pass.
Well at least most of them did.
Now all of them did.
OK.
Very very cool stuff.
So let's go ahead and let's write one more test here.
Before this video will be over and we're going to just make sure that our test when it downloads an
image that it's actually using the expected path because we have our host is good.
We would use that to check that our API is you know is working properly.
Make sure that our app is pointing to the right server and then we're going to make sure that it's using
the expected path.
So I just copied the test from above and I'm going to change this to uses expected path and the only
thing I need to change is the U R L here instead of using the image u r l.
I'm going to go ahead and actually write in a custom u r l here.
So yes colon backslash backslash.
And of course we know our host is images of pixels dot com.
But then if you look at the you or else here the path in order to get to photos is photos.
So we're going to go ahead and add that slash photos and basically that is going to be the L that we
are testing.
We're going to go ahead and check to make sure that your components drop path is then equal to slash
photos.
OK so all that we create We're going to make sure that it's actually pointing to the proper photo path.
So let's go ahead and let's build that so our diamond comes up let's click and build it.
Make sure that it actually runs and succeeds.
And we're going to basically check to see if it's using the expected path and look at that.
It is.
So that is amazing.
Very very cool stuff.
We have now basically created a mock session.
It is a mockup or an imitation of your L session that has the property you are l that we needed.
And it has the function data task Kaina.
Of course this is not all session you are all session if you go into here and into your session you'll
see there is a lot of functions lots of capabilities of your L session.
But we don't need all those We're only testing the data task feature and so we built our own custom.
Whereas it now we built our own custom.
Mark you are all session with all the stuff we needed which is really really really cool.
This is great for testing things that you don't own you can create your own I guess instance of them
and link them with the protocol which is very very very cool stuff.
So let's head over to the next video where we're going to continue learning about unit testing.
I'll see you there.