1
00:00:06,100 --> 00:00:10,230
Everybody what's going on the scale up with Dev slopes and in this video we're going to learn how to

2
00:00:10,230 --> 00:00:16,440
write tests for view controllers you can basically create an instance of if View Controller run test

3
00:00:16,440 --> 00:00:21,570
on it and even do things programmatically like send the actions for certain buttons to push so that

4
00:00:21,570 --> 00:00:26,400
you know that your buttons work on your controller without you even having to run and test it yourself.

5
00:00:26,400 --> 00:00:27,950
Very very cool stuff.

6
00:00:27,960 --> 00:00:34,490
So let's go ahead and pull up in our testing project here and this is our test for our calculator manager

7
00:00:34,500 --> 00:00:35,200
model.

8
00:00:35,340 --> 00:00:42,480
But why don't we go ahead and right click on walk Helka Aroney tests and click new file and another

9
00:00:42,480 --> 00:00:44,550
unit test case class.

10
00:00:44,550 --> 00:00:51,080
Except this time we're going to name it calculator View Controller tests.

11
00:00:51,390 --> 00:00:54,630
And of course that's going to be a subclass of X-C test case.

12
00:00:54,690 --> 00:00:57,320
Click next and then save it.

13
00:00:57,690 --> 00:01:02,330
And now we get the same thing we did before we get to example tests we can delete.

14
00:01:02,340 --> 00:01:08,620
We don't need them and we get a set up and tear down function k very cool.

15
00:01:08,640 --> 00:01:17,730
Now of course to access our actual app project we need to type at testable import wall Kalka Aroney

16
00:01:18,690 --> 00:01:21,890
And now we have full access which is very very cool.

17
00:01:21,900 --> 00:01:26,550
So what we're going to do is we're going to have to think OK what is the system we're trying to test

18
00:01:26,550 --> 00:01:27,390
here.

19
00:01:27,420 --> 00:01:28,320
It's our view controller.

20
00:01:28,320 --> 00:01:31,190
So we're going to go ahead and create our system under test.

21
00:01:31,380 --> 00:01:35,170
Var Sutt and it's going to be calculator View Controller.

22
00:01:35,300 --> 00:01:35,940
OK.

23
00:01:36,090 --> 00:01:41,310
Put an exclamation mark at the end saying yep we are definitely going to be in stanching instantiating

24
00:01:41,310 --> 00:01:45,370
this and giving it a value and we're going to do that in set up.

25
00:01:45,370 --> 00:01:51,450
So I don't know if you already have gone through and looked at this you probably haven't but this view

26
00:01:51,510 --> 00:01:59,280
controller is of type calculator view controller but it also has a storyboard ID of calculator View

27
00:01:59,280 --> 00:01:59,720
Controller.

28
00:01:59,730 --> 00:02:00,870
And we're going to use that.

29
00:02:00,870 --> 00:02:02,840
So go back into your tests.

30
00:02:02,850 --> 00:02:04,620
Open up the assistant editor and you know what.

31
00:02:04,620 --> 00:02:12,090
While we're here why don't we go ahead and load up the project and then go to the controller so we can

32
00:02:12,090 --> 00:02:14,220
look at these side by side.

33
00:02:14,230 --> 00:02:18,150
So let's go into our view controller tests and here we are.

34
00:02:18,300 --> 00:02:24,150
So in order to use the storyboard to instantiate a view controller we need to first create an instance

35
00:02:24,150 --> 00:02:32,950
of our storyboard so type let storyboard equals you storyboard and the name is main right main but storyboard

36
00:02:33,000 --> 00:02:37,690
It's right there and the bundle we don't really care about so we're just going to say nil.

37
00:02:37,860 --> 00:02:41,400
And then what we can do is we can instantiate this view controller.

38
00:02:41,490 --> 00:02:49,140
So we're gonna instantiate sat by typing storyboard dot instantiate controller with identifier.

39
00:02:49,200 --> 00:02:56,640
Now our identifiers the one we set and it's just the name of the controller calculator view controller.

40
00:02:56,850 --> 00:03:02,960
But this just creates an instance of view controller we need to force it to be of type calculated controller

41
00:03:02,970 --> 00:03:08,920
so go ahead and type as calculator controller just like that.

42
00:03:08,940 --> 00:03:14,210
Now we need to do is we need to basically set our system under test to be nil at the end of the test.

43
00:03:14,210 --> 00:03:21,060
So type sut equals nil and that's going to tear down and make it nil so that every time we run the test

44
00:03:21,090 --> 00:03:24,510
we have a fresh instance which is exactly what we want.

45
00:03:24,510 --> 00:03:31,410
So let's go ahead let's write our first test and let's begin let's think we have our little calculator

46
00:03:31,410 --> 00:03:32,250
here.

47
00:03:32,250 --> 00:03:33,780
What do we want to test.

48
00:03:33,810 --> 00:03:35,810
How about adding values.

49
00:03:35,850 --> 00:03:40,470
Should append those values to our array and label here.

50
00:03:40,650 --> 00:03:41,670
Let's let's test that out.

51
00:03:41,670 --> 00:03:45,580
So let's type phunk test adding values.

52
00:03:45,690 --> 00:03:47,080
That's what we're testing.

53
00:03:47,220 --> 00:03:52,490
And the result is should append and update label.

54
00:03:52,570 --> 00:03:55,930
And I'm going to go ahead and fix that capital P there.

55
00:03:56,040 --> 00:03:56,730
Cool.

56
00:03:56,730 --> 00:03:57,180
Awesome.

57
00:03:57,180 --> 00:04:01,180
So let's go ahead and let's access our system under test.

58
00:04:01,590 --> 00:04:05,520
And the cool thing is right in here we can access all of our buttons.

59
00:04:05,520 --> 00:04:11,940
So let's go ahead and let's just pretend that we're going to go ahead and add 2 5 K to make 25.

60
00:04:11,940 --> 00:04:16,420
So let's type set that 2 button and here's what we can do.

61
00:04:16,470 --> 00:04:24,360
We can type send actions for a particular UI control event and these UI button outlet's if we push on

62
00:04:24,360 --> 00:04:27,730
one what we're doing is we're calling touch up inside.

63
00:04:27,750 --> 00:04:33,060
So basically by typing this we are simulating what happens when you actually touch the button.

64
00:04:33,090 --> 00:04:35,420
Very very cool we don't even have to do it ourselves.

65
00:04:35,460 --> 00:04:39,560
It can test this in just one second or less than that very very fast.

66
00:04:39,840 --> 00:04:43,160
So what we want to add.

67
00:04:43,590 --> 00:04:50,940
OK so we we tap the two button we want to add another value so type set five button ok send actions

68
00:04:50,940 --> 00:04:53,090
for Dot touch up inside.

69
00:04:53,100 --> 00:04:56,160
So we have now pushed 2 and 5.

70
00:04:56,340 --> 00:05:04,490
And now what we want to do is we want to actually check to see is our label actually 25.

71
00:05:04,500 --> 00:05:08,650
We've written code to this but we're basically testing the view controller to see if it's displaying

72
00:05:08,710 --> 00:05:10,720
properly when the user interacts with it.

73
00:05:10,720 --> 00:05:18,220
So we can type ex-city assert equal sut dot and this label here is called value label.

74
00:05:18,220 --> 00:05:24,990
So we can type set that value label text and it should be equal to 25.

75
00:05:25,360 --> 00:05:30,130
So let's go ahead and let's run this test and let's see if it properly is doing what it's supposed to

76
00:05:31,120 --> 00:05:34,810
build and run and test as well.

77
00:05:34,810 --> 00:05:41,020
So there we go it tested perfect and it's testing.

78
00:05:41,050 --> 00:05:44,880
And did we pass that seeded with bats.

79
00:05:44,950 --> 00:05:45,930
Are we going to pass.

80
00:05:46,030 --> 00:05:47,010
He's going to let me pass.

81
00:05:47,260 --> 00:05:48,050
Oh no.

82
00:05:48,050 --> 00:05:50,320
So here is a problem.

83
00:05:50,320 --> 00:05:51,310
Check it out.

84
00:05:51,340 --> 00:05:52,220
What is the error.

85
00:05:53,250 --> 00:05:54,030
All right.

86
00:05:54,030 --> 00:06:02,120
So the test started then we get an error and it's going to tell us hey unexpectedly found nyl while

87
00:06:02,120 --> 00:06:04,010
unwrapping an optional value.

88
00:06:04,050 --> 00:06:11,160
Now if you have ever set up an action or an outlet and linked it to something and then deleted it or

89
00:06:11,160 --> 00:06:18,090
deleted the item and it was still referencing it it basically shows up as nil because it's not yet instantiated

90
00:06:18,120 --> 00:06:26,190
but items linked by outlet's or Iby actions are instantiated by the view controller upon which they're

91
00:06:26,190 --> 00:06:27,480
connected to.

92
00:06:27,480 --> 00:06:31,510
So we actually didn't do something that's very very important.

93
00:06:31,540 --> 00:06:39,460
Now if you think about it when your app launches feuded load is a very important function that's called

94
00:06:39,460 --> 00:06:40,760
because that.

95
00:06:40,780 --> 00:06:46,870
Well I guess feuded load you will appear any of those basically are called different times and you can

96
00:06:46,870 --> 00:06:51,450
basically set up things to be you know be initialized to be set up initially.

97
00:06:51,700 --> 00:06:57,690
And we have not you know called feuded load because this isn't running on an actual device.

98
00:06:57,700 --> 00:07:04,470
So what we can do is in our setup function we can type set dot load view if needed.

99
00:07:04,690 --> 00:07:10,390
And it says load's the view controllers view if it has not been loaded which it never does so this loads

100
00:07:10,390 --> 00:07:12,000
it for the first time.

101
00:07:12,010 --> 00:07:14,110
Now let's go ahead and try our test.

102
00:07:14,200 --> 00:07:18,370
And now we should have full access to our buttons because we have called feuded load and everything

103
00:07:18,370 --> 00:07:20,110
is initialized properly.

104
00:07:20,110 --> 00:07:27,580
So here we go it's testing and success our test passes our buttons when we push to five.

105
00:07:27,610 --> 00:07:32,710
The label is successfully update the label is successfully updated.

106
00:07:32,710 --> 00:07:36,990
And you know what I just realized our test is testing two things.

107
00:07:37,030 --> 00:07:39,190
So let's just make sure should update label.

108
00:07:39,220 --> 00:07:45,090
That's what our test is for if we wanted to check the the array deep within the calculation manager

109
00:07:45,100 --> 00:07:47,260
we should have written a test for the calculation manager.

110
00:07:47,350 --> 00:07:48,560
And you can always go back and do that.

111
00:07:48,580 --> 00:07:49,720
It's up to you.

112
00:07:49,750 --> 00:07:52,550
So let's keep going let's write a few more tests here.

113
00:07:52,570 --> 00:07:58,880
Let's make sure that the calculation if we actually do a calculation that it's going to actually you

114
00:07:58,880 --> 00:08:03,490
know perform a calculation and update the label the right way so phunk.

115
00:08:03,610 --> 00:08:13,300
How about test equals button and what is what is our expected result should perform calculation and

116
00:08:13,390 --> 00:08:15,580
update label.

117
00:08:16,090 --> 00:08:17,320
OK.

118
00:08:18,280 --> 00:08:19,050
There we go.

119
00:08:19,360 --> 00:08:23,410
And so what we're going to do is we're going to run through what a user would do in order to actually

120
00:08:23,460 --> 00:08:24,980
you know pass in values.

121
00:08:25,000 --> 00:08:27,430
So I'm going to go ahead and just use the same thing here.

122
00:08:27,590 --> 00:08:29,880
We've passed in a two and a five.

123
00:08:29,920 --> 00:08:37,830
Now we're going to do sut maybe about subtract button that send actions for touch up inside.

124
00:08:38,260 --> 00:08:41,500
And so we're doing 25 minus sut.

125
00:08:41,560 --> 00:08:49,400
Dot How about I don't know 5 or well I guess 5 button whips that send actions for a touch up inside.

126
00:08:49,420 --> 00:08:54,540
And now we're going to call sut equals button because we have typed to 5.

127
00:08:54,590 --> 00:08:56,740
So 25 minus five.

128
00:08:56,740 --> 00:09:02,050
And when we push equals we should get a particular result should be 20.

129
00:09:02,050 --> 00:09:11,140
So let's type ex-city X C-T assert equals Surt dot value label dot text and it should be equal to a

130
00:09:11,140 --> 00:09:13,570
string with 20 inside.

131
00:09:13,570 --> 00:09:18,100
So let's go ahead and build to get our little diamond here and run that test.

132
00:09:18,160 --> 00:09:19,620
Make sure that it works.

133
00:09:20,440 --> 00:09:24,170
OK let's see we're testing and it comes back.

134
00:09:24,180 --> 00:09:26,200
GREENE It works.

135
00:09:26,220 --> 00:09:29,830
Let's just make sure that all of our tests work by pushing command you.

136
00:09:30,040 --> 00:09:34,970
You want to run all of your test as often as possible to make sure that one test didn't break another

137
00:09:35,220 --> 00:09:39,420
or that if you changed code it didn't break your other test both Test Pasto So that's great.

138
00:09:39,420 --> 00:09:42,120
So our calculation is working flawlessly.

139
00:09:42,120 --> 00:09:43,140
That's awesome.

140
00:09:43,530 --> 00:09:44,100
How about this.

141
00:09:44,100 --> 00:09:53,070
Let's test if changing the operand actually changes the operand so phunk test operand buttons should

142
00:09:54,340 --> 00:09:55,260
show.

143
00:09:55,620 --> 00:10:01,030
Change operand when tapped.

144
00:10:01,080 --> 00:10:02,150
That will work.

145
00:10:02,160 --> 00:10:07,100
So let's go ahead and let's make sure that our operand actually changes.

146
00:10:07,320 --> 00:10:09,670
Let's type sut dot.

147
00:10:09,690 --> 00:10:11,140
How about for Button.

148
00:10:11,190 --> 00:10:14,960
Let's say we type we want to do 4 plus 4.

149
00:10:14,970 --> 00:10:18,970
So send actions send actions for touch up inside.

150
00:10:19,320 --> 00:10:23,540
And if we're going to do plus we're going to go ahead and type sut dot.

151
00:10:23,550 --> 00:10:27,320
But let's say we accidentally type the multiply button first.

152
00:10:27,390 --> 00:10:35,430
So touch up inside Surt dot and now we're going to type the add button does send actions for a touch

153
00:10:35,430 --> 00:10:36,500
up inside.

154
00:10:36,690 --> 00:10:41,510
Then we're gonna type the four button again but send actions.

155
00:10:41,820 --> 00:10:46,800
And so now what we've done is we've pushed the four button with accidentally push multiply wups push

156
00:10:46,800 --> 00:10:48,860
the add button then push for.

157
00:10:48,900 --> 00:10:50,150
Now we're going to make sure.

158
00:10:50,250 --> 00:10:53,750
Oh whoops we've got a call equals that dot equals button.

159
00:10:54,060 --> 00:10:58,670
Send actions for touch up inside an ex-city assert equals.

160
00:10:58,830 --> 00:11:00,350
OK we're going to test the label.

161
00:11:00,360 --> 00:11:04,950
So value label that text and it should be 8 right.

162
00:11:04,950 --> 00:11:06,030
Four plus four.

163
00:11:06,030 --> 00:11:07,710
So let's go ahead and build this.

164
00:11:07,710 --> 00:11:12,420
We get our testing diamond and let's run that test let's make sure that this works the way that we are

165
00:11:12,420 --> 00:11:13,480
hoping.

166
00:11:13,890 --> 00:11:21,210
Testing testing and it works so changing the operand when we tap it actually successfully modifies the

167
00:11:21,210 --> 00:11:21,920
operand.

168
00:11:21,930 --> 00:11:23,100
That's amazing.

169
00:11:23,100 --> 00:11:29,260
So let's see how about let's test our a button.

170
00:11:29,340 --> 00:11:39,150
So phunk test we'll just call it the clear button should clear label because that's what we should do.

171
00:11:39,180 --> 00:11:42,570
Clear the label and let's let's check that out.

172
00:11:42,600 --> 00:11:50,740
So let's see sut dot 8 button send actions for touch up inside.

173
00:11:50,880 --> 00:11:58,610
So we tap the eight button let's do Sutt dot seven buttons send actions for touch up inside.

174
00:11:58,610 --> 00:12:00,280
And now let's do it.

175
00:12:00,380 --> 00:12:03,500
A See button that sends actions.

176
00:12:03,500 --> 00:12:08,390
And if we did this properly our value in our label should now be zero.

177
00:12:08,390 --> 00:12:17,720
So ex-city assert equals and let's go ahead and say some value label dot text should be zero.

178
00:12:18,060 --> 00:12:26,930
OK let's run that test and see how we did launching our tests running our tests and it passes.

179
00:12:26,990 --> 00:12:27,900
Yay.

180
00:12:28,100 --> 00:12:29,800
But let's think OK.

181
00:12:29,810 --> 00:12:34,720
The label shows up but if I want to make sure that all of my values are zero.

182
00:12:34,820 --> 00:12:38,860
Let's make sure you know everything works the way it's supposed to.

183
00:12:38,870 --> 00:12:44,510
We don't have to because we've already done this in our calculation manager test that's a test for our

184
00:12:44,690 --> 00:12:45,510
data model.

185
00:12:45,620 --> 00:12:50,620
So when clear is called K all the values are set to zero.

186
00:12:50,810 --> 00:12:53,990
And we know that when.

187
00:12:54,290 --> 00:13:00,860
Whereas our test here we know that when the clear button is pressed in our code AC button we call clear

188
00:13:00,860 --> 00:13:01,230
value.

189
00:13:01,230 --> 00:13:02,690
So we already know that works.

190
00:13:02,690 --> 00:13:03,900
So we don't have to worry about it.

191
00:13:03,950 --> 00:13:05,180
Now you could write a test.

192
00:13:05,180 --> 00:13:08,410
It's up to you but it's not necessary.

193
00:13:08,420 --> 00:13:12,510
Now we have written tests for adding values.

194
00:13:12,630 --> 00:13:18,740
It updates the labels calculations performed properly operands change properly labeled clearing happens

195
00:13:18,740 --> 00:13:19,610
properly.

196
00:13:19,610 --> 00:13:25,460
Now if anyone were to come in here and change anything right like maybe I just said OK calculation manager

197
00:13:25,790 --> 00:13:28,170
pen let's set everything to four.

198
00:13:28,490 --> 00:13:29,030
OK.

199
00:13:29,180 --> 00:13:33,860
Now for if you look at the opera ending that means that basically it's going to be forced.

200
00:13:33,860 --> 00:13:35,570
Everything will be forced to divide.

201
00:13:35,630 --> 00:13:39,080
And if I build this the code technically works.

202
00:13:39,080 --> 00:13:42,830
So the build succeeds but if I push command you to run all my tests.

203
00:13:42,830 --> 00:13:44,060
Watch what happens.

204
00:13:44,060 --> 00:13:45,720
The test will launch.

205
00:13:45,740 --> 00:13:48,830
They're going to test but we're going to get a failure in our test.

206
00:13:48,830 --> 00:13:49,740
Check it out.

207
00:13:50,400 --> 00:13:54,000
Adding value should update label to five.

208
00:13:54,110 --> 00:14:00,570
OK it didn't work OK because we did not properly add in the number.

209
00:14:00,820 --> 00:14:04,650
Oh actually you know what this is for the number button I just realized so we're basically forcing every

210
00:14:04,650 --> 00:14:06,800
number to come in as the number four.

211
00:14:06,810 --> 00:14:10,210
So of course this test passes that means I broke all my buttons.

212
00:14:10,290 --> 00:14:16,020
This of course failed because I did 4 4 4 instead of two 5 and five.

213
00:14:16,020 --> 00:14:18,690
So now if I go back whoops and I fix that.

214
00:14:18,780 --> 00:14:21,890
Let's run our tests and check it out.

215
00:14:21,900 --> 00:14:23,170
The test will launch.

216
00:14:23,190 --> 00:14:25,350
They'll run and they'll be fixed.

217
00:14:25,350 --> 00:14:31,560
This is why testing is so so so so helpful and if you write a lot of tests for lots of things like this

218
00:14:32,130 --> 00:14:35,910
you'll be much much less likely to run into headaches and problems later on.

219
00:14:35,910 --> 00:14:41,210
So this is basically how to test controllers for various things.

220
00:14:41,280 --> 00:14:46,030
You of course can even test things whether you know a certain view appears on the screen.

221
00:14:46,270 --> 00:14:51,060
UI testing involves the actual display of information on the screen visually.

222
00:14:51,060 --> 00:14:51,990
And we'll test that later.

223
00:14:51,990 --> 00:14:54,210
But this is how you tested your controllers.

224
00:14:54,210 --> 00:14:55,640
It's very straightforward.

225
00:14:55,650 --> 00:14:57,980
Basically you think through what the user would do.

226
00:14:58,050 --> 00:15:03,110
You make sure that that action actually results in the result you were hoping for.

227
00:15:03,210 --> 00:15:06,450
Very very easy stuff guys but very very very powerful.

228
00:15:06,450 --> 00:15:08,430
So let's head over to the next video.

229
00:15:08,430 --> 00:15:10,350
This is Caleb with Dev slopes dot com.
