00:08
real world behaviors with them. We're going to pivot here today by instead testing the service and its methods itself.
00:15
So let's go ahead and jump into our free tab or you can stop your watch command there and run node ace make test and we can call this calculator underscore service and put it inside
00:25
of our unit suite. Before we hit enter on this though, first I have a typo, but second, if we should need
00:31
to test additional services beyond just this one, it may be pertinent for us to add into
00:36
our tests unit folder, another folder specifically for our services so that they're all grouped together.
00:43
We can do that by jumping in front of our specifications, name of calculator service and prefixing it with services slash similar to just specifying a file path.
00:53
And I'm going to go ahead and fix my typo there as well, and now we can hit enter to create that. Let's go and clear that out. Jump back over into our watch tab.
01:01
And now within our file tree, we should see a new services folder within our test unit folder. And if we expand that, there is our calculator service specification.
01:09
What we're going to do with this is test inside of our app services folder, the calculator service file right here. It's rather simple.
01:16
It has an add, subtract, multiply, and divide method inside of it that simply does the operation of the method name. So multiply and multiply, subtract and subtract, and add and add.
01:24
With our divide, however, if we attempt to divide by, which is our B parameter, zero, then it's going to throw an exception of division by zero is not allowed.
01:34
So within our calculator service specification, what we're going to want to do is test all of these methods as well as this division by zero to ensure that we do get that exception as we would expect.
01:44
So let's jump back into our specification and we can start with our addition. So we'll want to test that our calculator service should successfully add two numbers
01:54
by putting our specification inside of a services folder that has also added that folder's name to the prefix of our group's name.
02:02
And then our calculator service specification name is here as well. So we can omit that from our test as that will be used to specify what it is that we're testing for each test within this group.
02:12
So I'm going to go ahead and hide our sidebar away. And what we'll want to do is instantiate an instance of our calculator service.
02:19
So new calculator service and import that from services calculator service. This is where we have our calculator service dot add division, multiply and subtract methods.
02:29
Since we're testing add in this one, we can call this and it takes in two different numbers as its two parameters. So we can pass, say two and three in and what we should get back as our results is then
02:39
five. So we'll want to assert against that to ensure that our result, which is our actual value is equal to five.
02:48
And if we give that a save, then we'll see that that succeeds over here. Now you'll notice that I made our five a string because we're dealing with two numbers. What we would expect to get back as a number here as well.
02:58
So we can also test that with our assertion. But right now we're just doing a loose equality check, which is not going to check types.
03:06
So our number of five is coming through as equal to our string of five. Remember to add types to this, we would instead want to do a strict equality check, which
03:15
is akin to the three equal signs. So with that simple change, if we give this a save again, now we're failing because a number does not equal string.
03:24
So if we switch this to a number, then we're now verifying that not only is our result five, but it's also a number of five.
03:32
Now we know that this works a-okay with this specific scenario, but it's usually good to at least include two different iterations of this call because what if two plus three
03:41
is just a fluke by adding an additional add of say maybe negative one and one and asserting
03:48
against that, we're adding a little bit more validity to our successful state of the test because we know now two plus three is not a fluke because negative one and one also works
03:58
as well. Now, if you have a bunch of those scenarios, that's where data sets would come in handy here. However, addition, subtraction, multiplication, those are rather straightforward, so we can
04:06
just do them in line exactly like so. So we pass the result of our addition straight in as our actual value to the strict equal, and then we assert that we expect it to be five here.
04:16
We can then do the exact same. So assert strict equal, pass the result of our negative one plus one in, and we would expect that to then be zero. We can get rid of our final line down there.
04:25
If we give that a save, we should still be passing A-O-K, perfect. So we don't only have an addition method, but we also have a subtraction, multiplication,
04:34
and division method as well. So we'll paste this in one time for each of those for a total of four methods now that we're testing.
04:42
And we'll also want to test that this should successfully subtract two numbers, then we'll test that it should successfully multiply two numbers, and finally divide two numbers.
04:52
For each of these, we will mix the numbers up. So for our subtract here, we want to first switch this to our subtract method, and maybe
04:59
we want to subtract five and three to expect two, and then maybe two and four to expect negative two.
05:08
Then within our multiply test, we want to switch this to our multiply method, so multiply just like so.
05:13
And we might expect four and three to equal 12, and then negative two and three to equal negative six.
05:21
And then finally, for our division, we want to switch this to divide for both of our calls.
05:27
And then for the first one, maybe we want to divide 10 by two, which should give us five, and maybe 100 by five, which should give us 20. Perfect.
05:37
Let's give that a save, make sure that we're A-O-K on all of those, which we are. So we have all of these successful states of our methods covered. But remember that if we try and divide by zero, that's going to throw an error exception.
05:47
And we also want to test that as well. So let's copy and paste our divide one time, because this should instead throw exception
05:55
when dividing by zero. And for this, we can get rid of the two previous assertions that we had.
06:02
And if you recall back to our asynchronous lesson, you'll remember that try-catches can result in false positives.
06:10
So remember, if we do try-catch here, and we call our calculator service dot divide,
06:16
and we divide 10 by two, which is not dividing by zero, and we give this a save, it's still going to pass because we're never going to enter into the catch where we would have our
06:25
exception thrown assertions. So instead, what we would want to do is to assert that this throws, and then provide our
06:33
callback function in, which would then be our calculator service dot divide. And now if we do our 10 by two, we will no longer get our false positive.
06:41
So if we give this a save, now it's going to fail because it expected this function to throw an error.
06:47
If we then switch this to, say, something like seven divided by zero, then give that a save. Now it succeeds because it does actually throw that error.
06:56
Furthermore, with this, we can assert this specific error as well. So division by zero is not allowed. Give that a save.
07:04
And now we're also asserting that that message is thrown with the exception too. And perfect. Now we know for certain that our calculator service throws an exception when if we try
07:11
to divide by zero, and that it's able to successfully divide, multiply, subtract, and add two numbers together.