So let's just add in a unit test unit testing is extremely important for everything that you do.
No matter if you're really going to run it for a large audience in production on the permission test
network or an enterprise network unit testing is the one thing that you should not underestimate.
So I'm going to show you how you can actually read no less painful unit tests in an easy way and the
way I'm going to approach this is not only with Chai which is included in truffles.
So when you go to the truffle documentation and you go to the writing test in JavaScript and you see
on the bottom these tests and we wrote the tests before but I actually want to introduce a different
way of writing the test.
It looks exactly the same.
Just instead of a sort equal.
And then one really versus the other value you're going to use.
I expect this and we're going to also use Chai as promised to actually test promises if they are resolving
or if they are rejected.
So the first thing to do this is to install the necessary packages for your project.
So let me get out of this is exit.
So back to the command line and we are going to install chai chai a big number and Chai as promised
npm install safe chai chai by the end of the number and Chai as promised.
So they are installed.
This was fairly quickly.
And let's add our first test for our token go to test folder.
You failed my my token test that.
Yes.
So again you're going to import our artifact very very similar as in our deploy meant script or migration
script.
Is going to be token the effects of require my token.
Finally we need this sort of do at the end because truffle will take care of getting the right thing
and then the more important part is how we set up Chai.
So we have Chai is require Chai.
That's gonna require it from the milk modules folder and then we assign the big number from our free
to be used in Chai.
How are we going to use that.
We have a big number in maybe three deals the end that's the big number that we want to use.
And we have to change big number is require change and then big number that is not from myself.
That is something that I stole from the test from open Zeppelin as well as let me finish this and show
you where I got this from.
The same with Chai as promised.
That is something that I also stole from another project that works with all of the smart contracts
Chai as promised is require
and then Chai use
it promise.
And the last one is expected is expect
and then we have a token test
here inside we write the test.
OK.
So where did I get this from.
If you go to the
repository from open Zeppelin and you find the test folder then you find a token folder and inside over
here you'll find a lot of different tests and the way they set up the set up Che is with this opens
up in test helpers and that opens up adding test helpers are in the open Zeppelin well user in the opposite
but in this helpless repository and in source setup it took me a little bit of digging.
So it's well over here I already have the final result over to you.
That is how you set up the big number and how you use it within China within one of your tests where
free is automatically in check that Ian truffle test so you don't need to take care of this or you need
to do is configured Che in order to use it correctly.
Now I'm going to show you how you can write this test in order to make use of these extensions and that
is pretty pretty cool.
So have a look inside our contract's function.
We are going to write what you probably already know there.
It it what it should do something.
So it should be all tokens it should be in my account and this it function takes as a second parameter
a callback function.
And that's going to be an async
without any parameters.
And over here we are going to well define our test case.
So we have our instance is
can apply it.
This is how you get to talk an instance once it's deployed on the block chain and then we say total
supply is a way for instance the total supply.
So in the old version the version that is written on the truffle website would be well left.
Balance
is a way for instance that
balance off.
And then we need to
account 0 which is the account that deployed this might contract and then we usually say assert
equal balance value off or to number or two string or not.
And then initial supply of value off and then balance was not the same.
If it's not the same.
That is.
Well that's pretty lengthy.
And I don't like the style of writing it because when you see an he then you really have to always look
into what are the parameters.
I personally prefer not to this and to instead use expect because I expect from J.
Expect is a function that expects well something to be equal to something else.
And normally when you do balance and you always see this await keywords and this is because it's a promise.
It needs to resolve.
So normally we would have to do away instance of balance off and then count zero.
And over here is going to to be big number
equal total supply.
Let's run this but it can get even better.
So let's have a look.
If he can run this with truffle developer
test but most likely it will give us an error.
If you get this error then you have to start ganache what I'm doing right now
quick start.
Wait until the accounts are here.
Get out of this and have a look at our truffle confit fell.
We have one developed network but the default network is to develop better network and our port for
ganache is seventy five forty five seventy five forty five and the net worth is five seven seven.
Let's have a look if this works.
Truffle test each should find.
Oh and right called development.
You better love math if you have a development that's right.
That is the default network.
Let's see if that works.
Host 27 0 0 1 was the network idea.
5 7 7 7 OK
might looks good.
And we see our test goes through but we can also condensed is even more with this Chai as promised.
Let me show you the well this try as promised is actually.
So instead of something asynchronous with a promise you usually do something and then result.
And if not then something else with Chai as promised you can do something should eventually equal to
something else or something should be something else.
So if you have a promise and this resolves then should eventually equal or you can expect to be eventually
equal or to eventually have this property.
So how can we build this in expect then we don't have to wait here to
eventually be a big number.
And this eventually just takes care of waiting for this promise to resolve.
It should lead to the exact same result.
Check this
yes.
And it's done.
It's also a little bit faster.
That's it for this lecture in the next one we're going to add more tests and make sure our smart contract
works exactly how we wanted to work.