1
00:00:06,150 --> 00:00:09,030
Hey everybody has gone this Caleb with their slopestyle.

2
00:00:09,220 --> 00:00:11,710
And in this video we're going to continue unit testing.

3
00:00:11,710 --> 00:00:19,070
But this time we're going to be actually testing the server basically that our images are housed on.

4
00:00:19,180 --> 00:00:20,830
That's not our API it's not our server.

5
00:00:20,830 --> 00:00:27,880
But the cool thing is that we're going to be able to create a mock of you r l session and basically

6
00:00:27,910 --> 00:00:35,020
a mock is going to allow us to create an imitation our own version of your Else session but we'll be

7
00:00:35,020 --> 00:00:39,760
able to actually create it and slim it down basically to the most primary components that we actually

8
00:00:39,760 --> 00:00:41,320
need to test.

9
00:00:41,500 --> 00:00:47,560
And it just gives us access to it in a much easier way than needing to use the entire You are all session.

10
00:00:47,560 --> 00:00:54,400
So pull up your project and what we're going to do is we're actually going to create another file in

11
00:00:54,400 --> 00:00:57,010
the helper's folder called download client.

12
00:00:57,010 --> 00:01:02,740
So go ahead and right click click new file and then it's just going to be a basic swift file called

13
00:01:02,830 --> 00:01:05,210
download client.

14
00:01:05,630 --> 00:01:06,270
OK.

15
00:01:06,340 --> 00:01:11,160
Now create a class and name it download client Calixto.

16
00:01:11,410 --> 00:01:18,580
And what we're going to do is we are basically going to put one function and it's going to look familiar.

17
00:01:18,640 --> 00:01:25,300
It's going to basically be this same function download image with your URL so phunk download image with

18
00:01:25,360 --> 00:01:38,440
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

19
00:01:38,440 --> 00:01:40,710
just being a string we can change it to.

20
00:01:40,740 --> 00:01:50,350
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.

21
00:01:50,350 --> 00:01:53,360
Else we're going to go ahead and just call fatal error.

22
00:01:53,830 --> 00:02:01,690
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

23
00:02:01,690 --> 00:02:06,860
where we pass in the U R L and we get no data response and an error at the very end.

24
00:02:06,880 --> 00:02:08,710
Just like you are l session.

25
00:02:08,860 --> 00:02:14,980
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

26
00:02:14,980 --> 00:02:22,270
r l session and we're actually going to go ahead and create a variable called session of type u r l

27
00:02:22,270 --> 00:02:26,840
session and it's going to be equal to u r l session shared.

28
00:02:27,190 --> 00:02:35,440
Now inside our function we can call session data task and we can pass in our u r l here and we can push

29
00:02:35,470 --> 00:02:42,180
enter on our completion handler there and type in everything we need like data response and error.

30
00:02:42,190 --> 00:02:45,910
And then at the end we'll just go ahead and erase the code in there for now.

31
00:02:46,120 --> 00:02:52,150
And of course at the end of a day a task you need to resume's so that it actually completes and it knows

32
00:02:52,150 --> 00:02:53,490
when it's done.

33
00:02:53,500 --> 00:02:59,040
So basically we have a function that we pass a u r l into.

34
00:02:59,050 --> 00:03:05,100
We create an actual u l instance we create a data task from our session here.

35
00:03:05,350 --> 00:03:10,190
And then you know we call resume at the end so that it actually you know does what it's supposed to

36
00:03:10,190 --> 00:03:14,900
do and here will be code to create image.

37
00:03:15,070 --> 00:03:18,280
So if we try to build this you'll see that everything works.

38
00:03:18,280 --> 00:03:19,630
It builds successfully.

39
00:03:19,630 --> 00:03:25,850
The code is good but basically what we need to do is we need to test this.

40
00:03:25,990 --> 00:03:32,680
And so we're going to go ahead and right click click new file and create a unit test case class called

41
00:03:32,740 --> 00:03:34,890
download client.

42
00:03:34,930 --> 00:03:38,890
Client tests go ahead and save that.

43
00:03:38,890 --> 00:03:45,820
And then what we're going to do is we're going to get rid of all the beginner boilerplate stuff.

44
00:03:45,820 --> 00:03:53,360
We're going to import our project here by typing testable import photo load just like we have been.

45
00:03:53,620 --> 00:03:59,860
And then what we're going to do is we're going to go ahead and basically create an instance of our download

46
00:03:59,860 --> 00:04:06,610
client by typing var such as of type download client and then we're going to go ahead and set it up

47
00:04:06,670 --> 00:04:07,770
in set up.

48
00:04:07,780 --> 00:04:14,990
So go ahead and type the system under test is equal to download client and instantiate it.

49
00:04:15,220 --> 00:04:21,510
Then when we tear down we're going to say sut equals nil like so.

50
00:04:22,180 --> 00:04:30,070
So very cool we now have our download client setup in our test or in our test case.

51
00:04:30,130 --> 00:04:35,380
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

52
00:04:35,380 --> 00:04:41,110
going to show you how you should test your API to make sure that you know you're using the expected

53
00:04:41,110 --> 00:04:45,110
host and using the expected paths for particular endpoints.

54
00:04:45,130 --> 00:04:46,000
It's really really helpful.

55
00:04:46,000 --> 00:04:49,300
And you can actually do this even before the API is done.

56
00:04:49,300 --> 00:04:54,220
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

57
00:04:54,220 --> 00:04:56,080
just using a mock or else session.

58
00:04:56,080 --> 00:04:59,880
So let's go ahead and write this test funk.

59
00:04:59,960 --> 00:05:00,340
Funk.

60
00:05:00,370 --> 00:05:02,980
Test download.

61
00:05:03,560 --> 00:05:08,640
And we're testing to make sure that it uses the expected host.

62
00:05:09,040 --> 00:05:09,790
OK.

63
00:05:10,120 --> 00:05:15,160
Now what we are going to do is we're basically going to take our image you are l.

64
00:05:15,250 --> 00:05:20,950
We're going to call our download image function from our download client and we're going to go ahead

65
00:05:20,980 --> 00:05:28,900
and basically pull out your old components and then verify that the moral component for host is equal

66
00:05:28,900 --> 00:05:35,590
to images dot pixels dot com because in our helper's you'll see that is the host from our images we're

67
00:05:35,590 --> 00:05:38,610
going to make sure that it's actually using the right host.

68
00:05:38,620 --> 00:05:43,920
So go ahead and type let image you r l that's going to be equal to image or L strings.

69
00:05:43,930 --> 00:05:48,670
And we're just going to pull out the item at the third index because that's the image we've been using.

70
00:05:48,670 --> 00:05:55,390
Then we're going to call the set system under test download image with you are l we can pass in our

71
00:05:55,390 --> 00:05:56,870
image you are all there.

72
00:05:57,160 --> 00:06:05,200
And now we can actually create an instance of your elso guard let your L equals you r l from string

73
00:06:05,900 --> 00:06:11,020
K like so and we're going to pass in the image you are l.

74
00:06:11,020 --> 00:06:16,070
Otherwise we're going to call ex-city fail since we're in a test and then return.

75
00:06:16,250 --> 00:06:18,060
OK we've done that before.

76
00:06:18,130 --> 00:06:21,920
Next we're going to go ahead and create the components from this u r l.

77
00:06:21,950 --> 00:06:31,230
So Type let your L components equals u r l components and we can basically use you r l k.

78
00:06:31,580 --> 00:06:34,380
And we want to resolve against the base you are l.

79
00:06:34,460 --> 00:06:35,930
That's true.

80
00:06:35,990 --> 00:06:43,990
So what we're going to be testing for is equivalency so X-C assert equal and we're going to check your

81
00:06:44,080 --> 00:06:45,820
components.

82
00:06:46,000 --> 00:06:51,240
Host K because we're checking to make sure that the host is equal to what it's supposed to be.

83
00:06:51,350 --> 00:06:56,150
And if you remember it should be images dot pixels dot com.

84
00:06:56,150 --> 00:06:59,000
So if I run this this test should pass.

85
00:06:59,000 --> 00:07:05,190
But the interesting thing is that we have not actually properly set up a mock your L session.

86
00:07:05,330 --> 00:07:07,080
And so we're going to do that now.

87
00:07:07,160 --> 00:07:13,910
And so we're going to extend our our download client tests and we're going to basically create a new

88
00:07:13,910 --> 00:07:15,680
class called mock you are all session.

89
00:07:15,680 --> 00:07:20,510
So go ahead and type extension download client tests.

90
00:07:20,690 --> 00:07:21,550
OK.

91
00:07:22,070 --> 00:07:30,620
And we're going to go ahead and create a new class called mock session and it's going to basically have

92
00:07:30,680 --> 00:07:36,530
a couple of properties you know one that we need the most is yourself so type var you are all that is

93
00:07:36,530 --> 00:07:43,170
of course of type you are l and it's optional because it's not you know given a value until it's initialized.

94
00:07:43,430 --> 00:07:48,140
And so then what we're going to do is we're going to go ahead and recreate the function data task with

95
00:07:48,140 --> 00:07:58,850
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

96
00:07:58,880 --> 00:08:03,660
is escaping and of course we have data.

97
00:08:03,800 --> 00:08:06,560
We have a moral response.

98
00:08:07,190 --> 00:08:10,790
And we have what is it an error I think.

99
00:08:10,790 --> 00:08:18,410
And then of course the return there is void and the function itself data task returns a your L session

100
00:08:18,800 --> 00:08:20,680
data task can't.

101
00:08:20,930 --> 00:08:21,850
So there's that.

102
00:08:21,860 --> 00:08:28,460
And then what we're going to do is when this is called we're actually going to set self your L K for

103
00:08:28,460 --> 00:08:29,410
the session.

104
00:08:29,540 --> 00:08:34,400
We're going to set that to be equal to whatever you are l we pass in when we call data task.

105
00:08:34,400 --> 00:08:38,360
OK so then the session then has a u r l initialized.

106
00:08:38,660 --> 00:08:48,650
And then what we're going to do is return your L session shared data task whoops data task with you

107
00:08:48,660 --> 00:08:49,170
Earl.

108
00:08:49,310 --> 00:08:52,250
And we can pass and the u r l from r u r l session.

109
00:08:52,970 --> 00:08:53,870
That's how that's going to work.

110
00:08:53,870 --> 00:08:58,030
So if we build this it's going to go ahead and go away the errors.

111
00:08:58,040 --> 00:09:01,700
And now we have what is called a mock u r l session.

112
00:09:01,700 --> 00:09:06,830
This is basically your L session but with the function that we're actually testing and the only property

113
00:09:06,830 --> 00:09:07,730
that we really care about.

114
00:09:07,730 --> 00:09:13,760
So that's what's cool is we can basically create a smaller much much easier to test version of your

115
00:09:13,860 --> 00:09:17,960
session to make sure that it's working the way that we want it to work.

116
00:09:17,960 --> 00:09:22,920
Now this might seem silly but basically we're testing to make sure your L session works.

117
00:09:22,920 --> 00:09:26,900
You know but Apple built your L session so we're basically going to create our own to make sure that

118
00:09:26,900 --> 00:09:30,550
it's working the way we want in our particular use case.

119
00:09:30,560 --> 00:09:36,260
So there are a few things that we still need to do what we need to actually do is to make sure that

120
00:09:36,320 --> 00:09:44,440
when we use our mock you are all session that it's actually able to access this function from your session

121
00:09:44,450 --> 00:09:51,120
and the way we can do that is with a protocol so we can go ahead and type protocol session protocol.

122
00:09:51,560 --> 00:09:52,660
That's what we're going to name it.

123
00:09:53,060 --> 00:09:57,230
And the function we're just going to basically take the function that we wrote here.

124
00:09:57,500 --> 00:10:05,010
I'm going to copy it and then paste it in K because that is the function we are passing and you are

125
00:10:05,030 --> 00:10:05,560
l.

126
00:10:05,810 --> 00:10:09,650
We have our completion handler and that is the protocol that we need.

127
00:10:09,680 --> 00:10:16,370
And if we conform to this protocol our mock you are l session should be able to conform to it as well.

128
00:10:16,370 --> 00:10:21,120
So go ahead and create an extension of your Else session.

129
00:10:21,530 --> 00:10:27,390
OK you Arel session and we're going to conform to the session protocol.

130
00:10:27,650 --> 00:10:28,890
OK very very cool.

131
00:10:29,060 --> 00:10:35,840
Now since this function is already a part of your session we don't need to actually type it in the extension.

132
00:10:36,080 --> 00:10:42,010
We can just go ahead and leave it as it is but where it definitely becomes important is in our mock

133
00:10:42,160 --> 00:10:43,100
you or all session.

134
00:10:43,130 --> 00:10:49,490
So go into your download client tests and what we're going to do is in order for mock your session to

135
00:10:49,490 --> 00:10:57,630
successfully utilize this data task function we're going to go ahead and conform to session protocol.

136
00:10:58,310 --> 00:11:04,270
And now we have access to this basically we've conformed to the protocol because we have added in data

137
00:11:04,280 --> 00:11:05,120
a task and you know what.

138
00:11:05,120 --> 00:11:10,790
If I were to actually get rid of this it would tell me that I'm not conforming to the session protocol

139
00:11:11,540 --> 00:11:13,030
in just a second it will get there.

140
00:11:13,040 --> 00:11:14,860
It'll tell me hey there we go OK.

141
00:11:14,900 --> 00:11:17,980
Undeclared use of type session protocol.

142
00:11:18,080 --> 00:11:24,470
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

143
00:11:24,470 --> 00:11:34,710
code back in here self-doubt you are l equals u r l and then return or session Scott shared dot data

144
00:11:34,710 --> 00:11:39,060
task with you l and pass in the L U r l.

145
00:11:39,540 --> 00:11:39,900
Cool.

146
00:11:39,900 --> 00:11:46,800
So we are now conforming to that mock are Session now is actually accessing the proper function from

147
00:11:47,640 --> 00:11:49,820
your session which is very cool.

148
00:11:50,190 --> 00:11:56,130
And basically we can now use this as the actual session that download client uses.

149
00:11:56,130 --> 00:12:04,080
So in download client go ahead and set up the session here not to be your whole session but instead

150
00:12:04,320 --> 00:12:06,980
to be Mach. you are all session.

151
00:12:07,590 --> 00:12:12,060
But what happens when I try to build that it's going to say hey you don't have access to that here.

152
00:12:12,060 --> 00:12:19,920
So what I can do instead is set this to basically conform to session protocol because it's going to

153
00:12:19,920 --> 00:12:24,470
be able to access either our mock you or all session or an ordinary session.

154
00:12:24,480 --> 00:12:28,370
But since we're using it with a mock your all session it's going to use that.

155
00:12:28,530 --> 00:12:29,600
So very very cool.

156
00:12:29,610 --> 00:12:35,520
That is now setup we can now go back and modify this to fit our needs a little bit better.

157
00:12:35,520 --> 00:12:44,250
So in our tests we're going to go ahead and set our client here to have a mock session and so we need

158
00:12:44,250 --> 00:12:50,860
to go ahead and create one mock you or else session of type mock your session.

159
00:12:51,180 --> 00:12:54,540
Then we need to set it up and initialize it.

160
00:12:54,600 --> 00:12:58,500
Mock your all session equals mock all session and then check it out.

161
00:12:58,500 --> 00:13:06,540
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

162
00:13:06,540 --> 00:13:11,520
mocke and this is really helpful for things like for libraries that are just really really big that

163
00:13:11,520 --> 00:13:14,060
have a lot of different functions and features and abilities.

164
00:13:14,220 --> 00:13:18,790
You can basically strip it down and test only the features and functions that you really need.

165
00:13:18,810 --> 00:13:26,100
And so we now have set our download client to have a session of our own maake which is perfect and you

166
00:13:26,100 --> 00:13:30,600
can basically mockup anything you want to make sure that it works the way that you're hoping.

167
00:13:30,660 --> 00:13:36,720
And now if we go ahead and run this it's going to use our mock or else session that's conforming to

168
00:13:36,730 --> 00:13:40,560
the session protocol that we built and it'll test it.

169
00:13:40,560 --> 00:13:45,060
It'll run and it looks like our test successfully pass.

170
00:13:45,150 --> 00:13:46,980
Well at least most of them did.

171
00:13:46,980 --> 00:13:48,200
Now all of them did.

172
00:13:48,240 --> 00:13:48,860
OK.

173
00:13:49,050 --> 00:13:50,250
Very very cool stuff.

174
00:13:50,250 --> 00:13:53,100
So let's go ahead and let's write one more test here.

175
00:13:53,100 --> 00:13:58,140
Before this video will be over and we're going to just make sure that our test when it downloads an

176
00:13:58,140 --> 00:14:04,500
image that it's actually using the expected path because we have our host is good.

177
00:14:04,530 --> 00:14:08,270
We would use that to check that our API is you know is working properly.

178
00:14:08,340 --> 00:14:14,280
Make sure that our app is pointing to the right server and then we're going to make sure that it's using

179
00:14:14,280 --> 00:14:15,570
the expected path.

180
00:14:15,570 --> 00:14:22,170
So I just copied the test from above and I'm going to change this to uses expected path and the only

181
00:14:22,170 --> 00:14:27,420
thing I need to change is the U R L here instead of using the image u r l.

182
00:14:27,420 --> 00:14:30,400
I'm going to go ahead and actually write in a custom u r l here.

183
00:14:30,480 --> 00:14:33,620
So yes colon backslash backslash.

184
00:14:33,810 --> 00:14:37,790
And of course we know our host is images of pixels dot com.

185
00:14:37,980 --> 00:14:44,310
But then if you look at the you or else here the path in order to get to photos is photos.

186
00:14:44,310 --> 00:14:50,520
So we're going to go ahead and add that slash photos and basically that is going to be the L that we

187
00:14:50,520 --> 00:14:52,150
are testing.

188
00:14:52,290 --> 00:14:58,890
We're going to go ahead and check to make sure that your components drop path is then equal to slash

189
00:14:59,330 --> 00:15:00,320
photos.

190
00:15:00,350 --> 00:15:06,340
OK so all that we create We're going to make sure that it's actually pointing to the proper photo path.

191
00:15:06,360 --> 00:15:10,740
So let's go ahead and let's build that so our diamond comes up let's click and build it.

192
00:15:10,740 --> 00:15:13,650
Make sure that it actually runs and succeeds.

193
00:15:13,960 --> 00:15:18,220
And we're going to basically check to see if it's using the expected path and look at that.

194
00:15:18,360 --> 00:15:18,960
It is.

195
00:15:18,990 --> 00:15:20,650
So that is amazing.

196
00:15:20,670 --> 00:15:21,860
Very very cool stuff.

197
00:15:21,870 --> 00:15:25,620
We have now basically created a mock session.

198
00:15:25,650 --> 00:15:32,620
It is a mockup or an imitation of your L session that has the property you are l that we needed.

199
00:15:32,820 --> 00:15:35,540
And it has the function data task Kaina.

200
00:15:35,550 --> 00:15:42,810
Of course this is not all session you are all session if you go into here and into your session you'll

201
00:15:42,810 --> 00:15:47,490
see there is a lot of functions lots of capabilities of your L session.

202
00:15:47,490 --> 00:15:53,680
But we don't need all those We're only testing the data task feature and so we built our own custom.

203
00:15:53,830 --> 00:15:56,300
Whereas it now we built our own custom.

204
00:15:56,340 --> 00:15:59,930
Mark you are all session with all the stuff we needed which is really really really cool.

205
00:15:59,940 --> 00:16:05,070
This is great for testing things that you don't own you can create your own I guess instance of them

206
00:16:05,070 --> 00:16:08,040
and link them with the protocol which is very very very cool stuff.

207
00:16:08,040 --> 00:16:12,850
So let's head over to the next video where we're going to continue learning about unit testing.

208
00:16:12,870 --> 00:16:13,610
I'll see you there.
