1
00:00:05,670 --> 00:00:08,790
Everybody what's going on this is Caleb with Deb's Lopes dot com.

2
00:00:08,800 --> 00:00:13,900
And in this video we're going to be showing an example of unit testing with swift.

3
00:00:13,920 --> 00:00:17,570
Ok now I'm going to show you an example of some code that I've written.

4
00:00:17,680 --> 00:00:23,410
I'm going to show you some tests that I wrote for that code and then I'm going to show you basically

5
00:00:23,650 --> 00:00:31,180
how a logic error could have been introduced in our application and how my unit test would basically

6
00:00:31,180 --> 00:00:35,270
prevent that from becoming a problem or would basically catch that issue.

7
00:00:35,290 --> 00:00:41,740
So I'm going to pull open a playground that I used here and what I've done is I've imported X C test

8
00:00:41,740 --> 00:00:43,010
which has X codes.

9
00:00:43,060 --> 00:00:44,770
Unit testing platform.

10
00:00:45,190 --> 00:00:49,810
And what I've done is I've basically created a simple class called post.

11
00:00:49,870 --> 00:00:53,840
Think of this like an Instagram post or a Facebook post or even like a tweet.

12
00:00:54,070 --> 00:01:00,490
Of course it's a very simple and incomplete example because there's only a time variable and a time

13
00:01:00,550 --> 00:01:02,160
ago variable.

14
00:01:02,170 --> 00:01:08,650
Think of this sort of like the time stamp of when it was posted and that time is belonging to this Inam

15
00:01:09,160 --> 00:01:12,190
which has different units of time.

16
00:01:12,190 --> 00:01:14,750
Seconds minutes hours days week month year.

17
00:01:14,830 --> 00:01:20,980
OK and they're of type string with a raw value of basically the string representation of themselves.

18
00:01:20,980 --> 00:01:27,670
So we have time ago we have time when it's initialized both those values are set.

19
00:01:27,880 --> 00:01:34,870
Then we have a secondary class called time manager and basically the time manager has a function called

20
00:01:34,870 --> 00:01:37,350
display time ago for post.

21
00:01:37,570 --> 00:01:38,320
OK.

22
00:01:38,440 --> 00:01:41,800
And what we're going to do is pass a post into that function.

23
00:01:41,800 --> 00:01:47,860
We're going to basically set a variable or I guess a constant called time ago and we poll the time ago

24
00:01:47,860 --> 00:01:49,720
value from our post.

25
00:01:49,840 --> 00:01:54,950
We have a pending message of s ago you'll see why that makes sense in just a second.

26
00:01:55,180 --> 00:01:59,500
And then what we do is we return the time ago.

27
00:01:59,650 --> 00:02:05,160
OK the display message so string version of time ago because time ago of course is.

28
00:02:05,180 --> 00:02:09,750
And so if it was you know 24 seconds 24 would be the value here.

29
00:02:09,910 --> 00:02:17,410
Then we add in a space programmatically then we take the time k the raw value of whatever the time is.

30
00:02:17,410 --> 00:02:21,070
So if it was seconds this would be the word second.

31
00:02:21,310 --> 00:02:28,840
And then we add the pending message s ago when it prints out we get 24 seconds ago that's where the

32
00:02:29,260 --> 00:02:30,740
space go comes in.

33
00:02:30,810 --> 00:02:32,280
It depends to the end.

34
00:02:32,290 --> 00:02:41,590
Now we have created an instance of post we've passed in 24 the time a unit is seconds so 24 seconds

35
00:02:41,650 --> 00:02:49,990
ago and the time manager is instantiated and then we call time manager dot display time ago for post

36
00:02:50,380 --> 00:02:55,870
and we print the returning value because of course this creates a string when it returns.

37
00:02:55,870 --> 00:02:57,810
Now this is our implementation code.

38
00:02:57,820 --> 00:03:02,290
Think of you know if you had it in a project or something and now I'm going to show you what it looks

39
00:03:02,290 --> 00:03:06,810
like when we actually create unit tests it'll be inside of an X code project.

40
00:03:06,820 --> 00:03:07,940
But the code is the same.

41
00:03:07,940 --> 00:03:16,060
So I've created a separate class called time manager tests and these tests are basically going to be

42
00:03:16,660 --> 00:03:22,300
for our time manager We're going to test all of the methods and whatever in our time manager class.

43
00:03:22,420 --> 00:03:28,360
Now it is going to inherit from X-C test case that is going to be a must every time.

44
00:03:28,360 --> 00:03:34,270
And basically what we're going to do is we're going to utilize a function called set up and a function

45
00:03:34,270 --> 00:03:35,490
called tear down.

46
00:03:35,590 --> 00:03:39,050
We're actually overriding them from the test case.

47
00:03:39,640 --> 00:03:46,210
But what happens is whenever our tests are run every time a test is run set up is called.

48
00:03:46,210 --> 00:03:52,060
And at the end of the test tear down is called now a set up is good for basically creating instances

49
00:03:52,060 --> 00:03:58,270
of things that we're going to need that we want available at a larger scope.

50
00:03:58,270 --> 00:04:02,440
OK so time manager we want available throughout all of our tests for a time manager.

51
00:04:02,440 --> 00:04:07,080
So we create a variable here and we set its value in set up.

52
00:04:07,210 --> 00:04:11,800
Then when we tear it down we're going to go ahead and set time manager to be equal to nil.

53
00:04:11,800 --> 00:04:15,630
We're basically just going to you know instantiate it and then DISTANCIA it.

54
00:04:15,640 --> 00:04:17,940
That's not a word but you know what I mean.

55
00:04:17,980 --> 00:04:21,480
After the test runs we have a fresh instance every single time.

56
00:04:21,490 --> 00:04:26,620
Now our test here is declared with a function and the beginning of the function is test.

57
00:04:26,620 --> 00:04:30,550
That's how X code knows that it is a test and not another type of function.

58
00:04:30,550 --> 00:04:33,200
So test time display.

59
00:04:33,340 --> 00:04:37,420
I like to write the word or are I guess what we're testing directly after test.

60
00:04:37,420 --> 00:04:43,600
So we're testing the time display and then at the end I put a little underscore and then I say what

61
00:04:43,600 --> 00:04:48,670
the expected result should be it should return a pending message for time.

62
00:04:48,670 --> 00:04:49,390
OK.

63
00:04:49,960 --> 00:04:54,530
And then inside of this test what I've done is I've created a separate instance of post.

64
00:04:54,820 --> 00:04:57,640
I have basically said 58 minutes.

65
00:04:57,760 --> 00:05:04,060
That's what I've passed in then we have created another constant called time ago message that basically

66
00:05:04,060 --> 00:05:10,810
uses our manager that's instantiated upon set up then we call display time ago for post and we pass

67
00:05:10,810 --> 00:05:16,570
in our post the message that gets returned should say 58 minutes ago.

68
00:05:16,720 --> 00:05:20,260
And that's where we use what is called ex-city assert.

69
00:05:20,260 --> 00:05:24,900
There are lots of different kinds of ex-city assert now not just equal.

70
00:05:24,910 --> 00:05:27,990
But there's also LCT assert not nil.

71
00:05:28,060 --> 00:05:32,430
And that verifies that something is not nale as it as you might expect.

72
00:05:32,860 --> 00:05:36,620
There is a certain bill which says yes that is nil.

73
00:05:36,640 --> 00:05:42,130
And you know we could test you know if a value has been properly cleared out or if a property is coming

74
00:05:42,160 --> 00:05:52,240
in as nil or if it is not nil I mean now there is a assert true or false except assert no throw except

75
00:05:52,330 --> 00:05:52,660
assert.

76
00:05:52,660 --> 00:05:57,490
Less then greater then there are a lot of different assertions we can make and basically what we're

77
00:05:57,550 --> 00:06:02,150
saying is we are asserting that a certain thing is true.

78
00:06:02,350 --> 00:06:05,710
OK so if something is false we can assert that it is false.

79
00:06:05,710 --> 00:06:10,480
But what I'm doing is I'm asserting to make sure that this message comes back saying.

80
00:06:10,480 --> 00:06:12,260
58 minutes ago.

81
00:06:12,490 --> 00:06:17,220
Then at the very bottom I called time manager tests default test suite and I run the test.

82
00:06:17,230 --> 00:06:23,640
Now when I actually run it you'll see that my post here 24 seconds it does successfully print out 24

83
00:06:23,650 --> 00:06:24,550
seconds ago.

84
00:06:24,970 --> 00:06:32,140
And our test suite here says the test started the test took point 0 0 3 seconds and we executed one

85
00:06:32,140 --> 00:06:37,990
test with zero failures zero unexpected in zero point zero three seconds.

86
00:06:37,990 --> 00:06:40,200
So that means our test passed.

87
00:06:40,300 --> 00:06:45,830
It means that the message that came back was equal to what we were expecting to get back.

88
00:06:45,850 --> 00:06:49,350
And that means our test works.

89
00:06:49,350 --> 00:06:50,930
K.R. code is good.

90
00:06:50,980 --> 00:06:55,710
Now this is where I'm going to introduce a problem into our application.

91
00:06:55,720 --> 00:06:56,820
OK.

92
00:06:57,070 --> 00:07:02,320
The thing that we're going to do is to show maybe if a junior developer came in and they wanted to change

93
00:07:02,320 --> 00:07:09,040
the code K they wanted to modify it and maybe they thought just you know incorrectly that this already

94
00:07:09,040 --> 00:07:12,160
had all of the values at the end.

95
00:07:12,160 --> 00:07:14,210
I'm going to undo that because it doesn't.

96
00:07:14,350 --> 00:07:19,180
But let's say they thought you know 24 seconds ago they thought Oh it already has the s.

97
00:07:19,240 --> 00:07:22,820
So I'm going to go ahead and this shouldn't be here let's get rid of that code.

98
00:07:22,870 --> 00:07:25,500
So it just says a go at the end.

99
00:07:25,600 --> 00:07:28,600
And of course you can see what printout is wrong.

100
00:07:28,780 --> 00:07:32,130
But look test suite started the test begins.

101
00:07:32,140 --> 00:07:35,080
And look at this example playground error.

102
00:07:35,210 --> 00:07:42,730
Test time display should return a pending message for time ex-city assert equal failed 58 minute ago

103
00:07:43,000 --> 00:07:47,060
is not equal to 58 minutes ago it was working.

104
00:07:47,140 --> 00:07:50,440
Our test passed but now our test fails.

105
00:07:50,440 --> 00:07:54,360
So that means hey there's something wrong in our code something that did work.

106
00:07:54,370 --> 00:07:56,560
At one point now fails.

107
00:07:56,680 --> 00:08:02,830
And so I can look at this error message and say hey 58 minute ago is not equal to 58 minutes ago and

108
00:08:02,830 --> 00:08:09,100
I can go back up here and I can say OK so this function that's supposed to be you know creating the

109
00:08:09,100 --> 00:08:10,370
proper message here.

110
00:08:10,390 --> 00:08:17,410
Oh the appending message is missing s space and now I can run the test again and you'll see all tests

111
00:08:17,410 --> 00:08:18,070
pass.

112
00:08:18,070 --> 00:08:20,830
The code is good so that caught that problem.

113
00:08:20,860 --> 00:08:27,580
If I were to build and run my app the thing that's tricky is that this will still run right just because

114
00:08:27,580 --> 00:08:30,370
the tests fail doesn't mean my project won't run.

115
00:08:30,370 --> 00:08:35,530
If I were to print this out maybe on Instagram below my post if I posted it 24 seconds ago it would

116
00:08:35,530 --> 00:08:41,290
look super unprofessional if it said 24 seconds ago with no space.

117
00:08:41,470 --> 00:08:45,630
So while it will still run it will still successfully compile.

118
00:08:45,700 --> 00:08:50,730
This could accidentally get pushed all the way to production without anybody noticing.

119
00:08:50,890 --> 00:08:55,750
If there weren't unit tests which is why unit testing is so so powerful.

120
00:08:55,750 --> 00:09:02,350
This is a very simple trivial example but there are plenty of instances where things like this could

121
00:09:02,350 --> 00:09:09,430
happen where you could introduce something that technically works but is very very not what you want

122
00:09:09,430 --> 00:09:09,740
to do.

123
00:09:09,760 --> 00:09:16,090
Like this displaying unprofessional text but imagine it was something as simple as this or imagine something

124
00:09:16,090 --> 00:09:23,080
as complex as authentication problems or you know persisting user data things that are a very very big

125
00:09:23,110 --> 00:09:25,290
impact on user experience.

126
00:09:25,300 --> 00:09:30,910
Imagine if those you know successfully passed the code works looks good but you broke something that

127
00:09:30,910 --> 00:09:34,270
did work in the past so that's why unit testing is super powerful.

128
00:09:34,300 --> 00:09:39,940
It helps us to prevent really stupid errors in the future by basically creating a backup of all the

129
00:09:39,940 --> 00:09:45,310
code that works in the past with all the tests that pass so that in the future when things are implemented

130
00:09:45,640 --> 00:09:50,140
we make sure all the tests still pass and make sure that everything still works the way that it's supposed

131
00:09:50,140 --> 00:09:50,290
to.

132
00:09:50,290 --> 00:09:53,580
So unit testing is amazing and super super helpful.

133
00:09:53,590 --> 00:09:55,680
I'm really excited to teach you more about it.

134
00:09:55,690 --> 00:09:57,990
So let's go ahead and let's fix our code here.

135
00:09:58,150 --> 00:10:01,930
Make our test pass and let's head over to the next video.

136
00:10:01,930 --> 00:10:03,900
This is Caleb with Dev slopes dot.com.
