- In this section,
we're going to see how to write comprehensive tests
for our BackAccount class.
So if you want to follow along, go into lesson 11.
We're in example three now, and it's a HelloWorld example.
Here's what it looks like.
Here's a quick reminder of the BackAccount class.
If you remember, I had a BackAccount class
and the BackAccount class had had a constructor
to initialize the back account's name, and balance,
and deposit method,
which added an amount to the back account,
a withdraw method to withdraw,
and a getter to return the balance.
Okay, so that code is the same as before.
What we're gonna do now
is see how to write more comprehensive tests
in back-account.spec.ts.
So I'll give you a quick summary here of what's going on
and then we'll go through the deep down.
So we've got two tests.
I've got a test for the deposit method
and the test with the withdraw method.
And I've got some code that I want to execute
beforeEach test is executed.
Okay, so let's take a closer look at that code.
That was my class
and here's my comprehensive tests for the BackAccount class.
So from the top,
import the BackAccount class from back-account.ts.
This test suite is for the BackAccount class.
Typically, each test needs to create a new object,
a new fresh BackAccount with a zero balance.
This is the kind of...
Each test should be independent of every other test.
In other words, create a new object every time
so that it isn't encumbered with any history
from the previous tests.
So I've implemented the day beforeEach method.
I've got a variable here in my test suite.
I've defined a variable which will point to a back account.
beforeEach test, I run this code.
I basically create a new BackAccount.
Oh, it's me. I have 1,000 pounds.
So each test will basically start off
with this fresh back account.
The initial balance will always be 1,000.
Okay, very nice.
Let's check, this test here is testing the deposit method.
Does the deposit method work?
Okay, well, let's see.
I've already created the BackAccount.
I invoked the deposit method.
Let's verify that after, the balance should be 1,200.
So this expect and to be, very much like Jest, isn't it?
Very similar.
Remember here, when you say acc1.balance,
that's calling the getter.
My BackAccount class has a get balance property
to return the balance.
It should be 1,200.
Likewise, let's check that the withdraw method works.
Again, it'll run this code first.
beforeEach test, it'll create a new back account.
So I'm back to 1,000 again.
If I try to withdraw 200,
then when I get the balance afterwards it should be 800.
Everything we've learned so far about Jest
in terms of matches and expect functions and so on,
we can still use using Jasmine, okay?
So that's great.
So what we need to do then is to run the tests.
So a couple of things,
just if you wanna follow along with this.
First of all, make sure that you do an npm install, okay?
Because then the zip version of my project,
I delete the node modules folder because it's too big.
So if you wanna follow along, npm install,
and give it a few minutes to download the libraries.
Once you've done that, then you can run the test harness.
You say ng test in watch mode, like that.
Start the Karma test runner. It'll compile your code.
It'll start Karma.
It'll open up a browser window,
run the tests, display the results in the browser window.
Okay, so we can get that going now.
This is what it's gonna look like.
That's the command I just ran.
Give it a few moments.
In the browser window you should see this, okay?
So we should find, my app component still has some tests
that we do need to have a look at,
but my BackAccount suite had the deposit test method
and the withdraw test method.
And that all seems to be working beautifully.
So that's quite reassuring.
Everything we've learned so far with Jest
applies equally well with Jasmine
when we're testing Angular code as well.