Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Now that we have the theory and jargon out of the way, let's craft some more unit tests of our own. We'll work in Objective-C for now, but switch to Swift in the next video.
-
0:00
Okay, here we are back inside our algorithm project.
-
0:03
In the last video, we created a test case, and we called it BasicTests.
-
0:08
We're gonna continue our work in there.
-
0:11
What we wanna try is something a bit more realistic than the last batch.
-
0:15
So how about we test this other method, here,
-
0:18
inside our Playlist class, this method down here.
-
0:23
What we're doing is we're gonna take in a dictionary of RGB colors and
-
0:28
we're gonna output a UI color.
-
0:31
As we see in the line above here within the init method,
-
0:34
we're gonna use that to set the background color.
-
0:38
Now for this test let's send a dictionary that we init in our set up
-
0:43
through the method and then make sure we get the results we expect.
-
0:47
So we'll head back over to our test case.
-
0:52
And let's add some properties.
-
1:07
Now inside our set up method, let's give those properties some values.
-
1:41
We can also set those properties back to nil in our tear down.
-
1:52
All right we've set the stage, now for the action.
-
1:55
Let's create a test which passes our color dictionary into the RGB color method and
-
2:00
checks that our result is the expected color.
-
2:05
We'll give ourselves a little space right here.
-
2:36
Okay, so here notice we're using the XCTAssertEqualObjects.
-
2:40
If you just say Equal, it won't work,
-
2:42
you need to use EqualObjects because we're comparing two UI color objects.
-
2:47
Now we'll just take one moment, and figure out why we're getting this error here.
-
3:02
There we go, we just had to expose the method in
-
3:06
the interface of Playlist, so Playlist.h.
-
3:10
Okay we can run our method now,
-
3:20
And great it passes.
-
3:23
Now you may be thinking Gabe that was hands down the worst magic
-
3:27
trick I've ever seen.
-
3:28
It's like I saw you stuff the rabbit in the hat before the show,
-
3:31
it isn't quite magic when you pulled it back out.
-
3:35
Well you're right, but here's the important part.
-
3:37
Let's say someone else on your team was cleaning up your code one day and
-
3:41
they thought they found a typo.
-
3:43
It was right down here in the Playlist.m, they said it looks
-
3:49
like you misspelled alpha it's actually spelled with an F.
-
3:54
Well when they did their code would still compile just fine and
-
3:58
then they merged into the main branch again just fine.
-
4:01
Well luckily the next time you ran this test
-
4:08
You'd find that it fails.
-
4:11
You see the resulting color won't equal the expected color,
-
4:14
since there is no key spelled alpha, A-L-P-H-A anymore.
-
4:20
So the resulting color wouldn't be right, it wouldn't match this key value here.
-
4:25
So luckily the unit test would catch that issue and
-
4:27
plenty of others before users start seeing transparent background colors.
-
4:32
And the color would be transparent, of course because it would just insert 0 for
-
4:37
the alpha value, which is transparent.
-
4:40
So two more things to note, the first is this,
-
4:43
the little issue we just uncovered points to a possible way to improve the code.
-
4:48
Namely by using constants rather than hard coded strings, here and
-
4:53
here in our original method, you could use enum's or constants to make
-
4:58
sure that you don't have string matching errors and spelling mistakes there.
-
5:02
Secondly, this particular test would fail but if you had another test,
-
5:08
let's say to ensure that the color was not nil, that one would still pass.
-
5:14
The color would still exist, it just wouldn't be the color you want,
-
5:17
it would be a transparent color.
-
5:20
This highlights the importance of writing a lots of unit tests even for
-
5:23
a single method like our color from dictionary method.
-
5:26
Since there are lots of ways a method could fail though often not fail so
-
5:31
completely that they just return nil.
-
5:33
So since we know that that test fails, let's go back into our code and
-
5:38
see why it failed.
-
5:39
It failed because we misspelled alpha here, or a colleague did, and
-
5:44
then we can test it again and make sure it passes.
-
5:50
Well that's funny, it didn't pass, let's go see why.
-
6:08
Okay great, it passed.
-
6:11
Let's try one more just for practice, shall we?
-
6:13
We'll stick with the same method since we know you often need to test the same
-
6:17
method from several perspectives.
-
6:19
So this time let's be sure that if we have a color dictionary that is missing
-
6:23
a value, as in completely missing one of the color or alpha elements in the array,
-
6:28
that we'll get nil back instead of an initialized UI color.
-
6:33
So we could write that like this.
-
6:46
Now inside we'll define our color dictionary array
-
6:50
without any green key value pair at all.
-
6:53
And as a result color,
-
6:56
which is the result of passing our
-
7:01
dictionary into RGB color method.
-
7:28
So here, we're going to test if we run our colorDictionary that we created
-
7:32
here without a green value through our method, we should get Not nil.
-
7:41
The test fails, it looks like the resultColor is NOT nil,
-
7:45
we did get a color back.
-
7:47
What the heck, is our test broken?
-
7:50
No, not at all, in fact this test just proved how useful unit tests can be.
-
7:55
Well, much like our misspelled alpha key from a moment ago,
-
7:58
the RGB method treats a missing value as a zero.
-
8:03
So it simply creates an RGB color with zero for the green value.
-
8:08
So what should we do?
-
8:09
Well, the best answer is one that should be determined by you and
-
8:12
the designer for the app.
-
8:14
But one idea would be to create some code inside our RGB method to return a default
-
8:19
color, probably one that would look good with the design of the app
-
8:22
in case that ever encounters a dictionary without the right values.
-
8:26
A really basic fix could look like this inside our RGB method.
-
8:48
So what we're saying here is if our color dictionary has less than four items in it,
-
8:54
let's just return this default color that we created here.
-
8:59
Now of course, this doesn't say which one is missing and
-
9:02
you might wanna do something much more elegant than this but
-
9:05
it sort of gets to the heart of why these can be really helpful.
-
9:11
We'll run it again.
-
9:17
And it fails again what's going on?
-
9:20
Well what's going on is that in the course of creating a test and
-
9:23
thereby discovering the limitations of our previous code,
-
9:26
we've now changed our code to be a bit more robust certainly not bullet proof.
-
9:31
But in actuality we now want to test that it is NOT nil because we just
-
9:36
came up with a way to return a value even when it's missing a value.
-
9:43
Now let's try it.
-
9:51
Okay great, now it passes.
-
9:53
So we know that if we send in a dictionary with a missing value, it won't return nil,
-
9:59
because it'll give us our default color that we added to that method.
-
10:04
Now just to be clear, this code was never meant to be bulletproof in
-
10:08
the first place, and the fix I threw in was not exactly robust.
-
10:13
If we wanted to really strengthen the RGB color method we'd probably want to build
-
10:18
in some mechanism to account for any missing value of the four RGB or alpha,
-
10:22
and use constants for keys and who knows what else.
-
10:25
I only bring up this example to highlight that the act of writing unit tests in and
-
10:30
of itself will often lead you to look more closely at your existing code,
-
10:34
prod its limits and wrestle with less obvious paths it might take.
-
10:38
It's somewhat ironic that while we strive for
-
10:41
a nice long list of green check marks in our navigator,
-
10:45
it's actually the red X's that will often lead you to the best possible code.
-
10:51
You now have the very basics of unit testing Objective C using XC Test, but
-
10:55
as you've probably guessed, it isn't quite enough to actually test the sophisticated
-
10:58
code we build and support everyday.
-
11:00
Take a minute to review what we've learned with a short quiz, and then meet me in
-
11:04
the next stage where we'll explore mocking stubbing and unit testing SWIFT code.
You need to sign up for Treehouse in order to download course files.
Sign up