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 a permissionless network or in 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 write less painful unit tests in an easy way.
And the way I'm going to approach this is not only with Che, which is included in truffle.
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 assert equal and then one value versus the other value we
are going to use che expect assertions and we are going to also use Che 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 stuff exit.
So back to the command line and we are going to install che gi big number and che as promised and PM
install save che try PN for a big number and GI as promised.
So they are installed.
This was fairly quickly and let's add our first test for our token go to test folder new file my token
dot test dot JS.
So again, we are going to import our artifact very, very similar as in our deployment script or migration
script.
So that is going to be token
artifacts that require my token.
Don't even need this D at the end because truffle will take care of getting the right thing.
And then the important part is how we set up Che.
So we have che is require che that's going to require it from the node modules folder and then we assign
the big number from our web three to be used in Che.
How are we going to use that?
We have the big number in web three utils.
N That's the big number that we want to use and we have to type.
Big number is require try n and then big number.
That is not from myself.
That is something that I stole from the test from open Zeppelin as well as well let me finish this and
I'll show you where I got this from.
The same with Che as promised.
That is something that I also stole from another project that works with all of the smart contracts.
China, as promised, is require.
And then I use
chart as promised.
And the last one is expect is I expect.
And then we have the token test.
And here inside, we write the test.
Okay, so where did they get this from?
If you go to the.
Repository from Open Zeppelin and you find the test folder and 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 is with this open Zeppelin test helpers and that opens up and in
test helpers are in the open Zeppelin well user in the open Zeppelin Test helpers repository and in
source setup.
It took me a little bit of digging so well over here.
I already have the final result over to you.
That is how you set up the big number and how you used it.
Within try within one of your tests have free is automatically injected in truffle test so you don't
need to take care of this.
All you need to do is configure chai in order to use it correctly.
And 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 function.
We are going to write what you probably already know.
The it it know it should do something so it should be all tokens should be in my account and this it
function takes as a second parameter and a callback function and it'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
token deployed.
This is how you get the token instance once it's deployed on the blockchain.
And then we say total supply is a wait
instance, the total supply.
So in the old version, the version that is written on the truffle website would be well let balance
is await instance dot balance off
and then we need the
account zero, which is the account that deployed the smart contract.
And then we usually say assert
equal balance that value off or to number or to string or whatnot and then initial supply the value
off and then balance plus not the same.
If it's not the same, that is well it's pretty lengthy and I don't like the style of writing it because
when you see an assert equal, then you really have to always look into what are the parameters.
I personally prefer not to do this and to instead use expect because expect from Che.
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 a weight instance of balance off and then account 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 we 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 config file.
We have one develop network, but the default network is the developer network and our port for ganache
is 75, 45, 75, 45 and the network IP is five, seven, seven.
Let's have a look.
If this works truffle test ich should find a network called development develop ment.
If you have a development network, that is the default network.
Let's see if that works.
Host 20 7001 was the network ID 5777.
Boy.
It looks good.
And we see our test goes through, but we can also condense this even more with this Chae as promised.
Let me show you the weather 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 child as promised, you can do something should eventually equal to something else, whereas 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.
Let's 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 are going to add more tests and make sure our smart contract works exactly how
we want it to work.