This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Let's take a look at the code we'll be unit testing.
Click on Downloads to download the starting code.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
In this course we'll see many
examples of how to write unit tests.
0:01
First let's take a look at
the code we'll be testing.
0:05
You may already be
familiar with this code.
0:08
It comes from the treehouse defense game
that you might have seen in other courses.
0:10
Let's quickly review some of the classes
in the treehouse defense game.
0:15
To start, we'll only be concerned
with just a few classes.
0:19
First we have the point class
which represents a single point
0:23
on a two dimensional grid.
0:26
Here we have the values for x and y,
0:28
a constructor that initializes those
values and a method named DistanceTo.
0:30
DistanceTo simply returns
the distance to another point.
0:36
The map class represents a space
where objects in the game can be.
0:41
It has a width and a height.
0:45
The width and
height must be greater than 1.
0:47
There's an OnMap method that
determines if a point is on the map.
0:50
The map location class represents
a point on the map and
0:55
is more specific to the treehouse
defense game than a normal point.
0:58
The constructor throws
an exception if someone tries to
1:02
create a map location
that isn't on the map.
1:05
It has a method named InRangeOf
which determines if the map location
1:08
is within a given distance
of another map location.
1:12
Finally there's a file named exception.cs,
which contains our custom exception types.
1:14
So far it only contains the out of bounds
exception that is thrown by the map
1:20
location constructor.
1:24
For right now this is enough code
to get started writing unit tests.
1:26
We'll build up the treehouse defense
game a bit more as we go along.
1:30
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up