06 Petshop - Unit testing

Learn Ethereum by Examples Part 2 Pet-Shop - Your first Dapp
12 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

Welcome, everyone to our second part of our pet shop series. In this session, we are going to test the contract which we developed in our last session. So, we'll have a overview of what unit testing is, we will write three unit test cases and we'll run those test cases and see the results of them. Okay. So, in our last session, we created our contract here and we put some logic in our contract right this sorry, this content right. So now what we want to do is we want to write test cases for this one.

So unit test testing is the testing wherein we test the code okay. Whatever we have developed in you know, by shaking at The logic report and then we cover test cases for each of the pieces of the logic right. So, in our case, we have this requirement where in our adopters, the adoption can happen for maximum 616 pets right? So we can create one unit test case which will take, okay, if if, you know we are below 16 or above 16 of our limit of adopters, okay? So that's, you know, one example of a unit test where we are going to test that functionality, okay. And similarly, we will have other test cases as well, okay, which we'll get into detail.

Okay. So to start with the test cases are written in this directory, the test directory, okay. So in our test directly, we are going to write our test cases. So let me Start with the first test here. We will have a single file and within that file, we'll have three methods to cover three different unit test cases. Okay.

Now, just like before, here also for testing, we already have the code available to us, you go to aetherium demo solution folder. In our picture tutorial, we'll go to the testing folder or another section directly. You have the tester. This is our file copied into this. Let's go through this file. So this is our unit test file.

Truffle gives you the framework wherein we can test our function. So, in order to test our contract, we need to import our contract into the test file. So, in this import you will see there is an option dot saw the contract which we wrote has been imported and then there are two other inputs that is for the truffle unit test framework and then we are creating another contract. So, for unit testing itself you know, we will create a contract which will be deployed on the blockchain and then we can do the unit testing. So, here we are writing the contract in that contract. What we are going to do is, first we are going to get the instance of we are going to store the instance of our contract into one variable.

Okay. So, since we imported our contract, we can create a variable okay. Adoption of type contract adoption and we can then assign it some value okay. In our case, we have this what you see the truffle framework provides you the deployed address sort of a contract which can give you the address of the deployed contract okay. So, in our case, if you want to get an address, we can use this method deployed address dot the name of the contract, it will return you that address. So this variable will hold the address.

So this then we are creating the first test case this test case is for checking whether user camp adopt a pet, okay, so in our contract we did put the logic that user gamma dog maximum 16 bit Okay. So, in this test guess what we are going to do is we are going to get the ID of the adoption variable okay. So, in our adoption in our adoption contract we had this variable okay adopters right which holds the value of all adopted pairs okay. So, in our test case what we are trying to do is we are trying to use the up adoption variable which holds the address of the adoption contract and trying to get the eight variable in the adult. Okay, so in our contract we have this variable and we call this variable as adopters Okay. Okay.

And then we had this method called a doc. Right? So this method was returning the pet ID off. If you provide it the pet ID, it will, it will return that pet it okay. So what we're doing here is we are calling that method with the eight bit ID okay? And what we expect is we expect the same value to be returned.

Because in our method, all we are doing is we are checking if that pet ID is between zero and 15. Okay, if it is not, then it will throw an exception, otherwise it will return the same ID okay. So in our case, since it is between zero and 15, the return ID value will be it OK, and then we are having an assertion. In this assertion, all we're doing is we are taking the return value with the expected value of A. And if that is the case, then this assertion will pass and this test case will also pass. So it's a really simple unit test is okay.

Not much in that okay, the second test case is we are testing the address of that opera. Okay. So, in this case, you remember, we have a variable which holds the address of the instance of adoption. Right, so here we have an option variable which holds that address okay and then we have this Key called the key word called this So, this returns this returns the value of the contract address okay for this instance So, this is the expected owner for this contract. The next thing is we are calling the adoption dot adopters eight okay and this will return the address of the owner okay and here we are having the similar assertion okay if the expected address is equal to the heart of directors Okay. Okay.

So the second one what we are doing is, so, in the first test case itself in the first function itself what will happen is this variable adoption variable will go and you know it will get an address okay from the blockchain because you know we are calling this method and setting the value and that address is getting stored in the adoption variable we can also refer to the value of the variable or the address through the this keyword Okay. Okay. So, what is the what we are doing in the next test case is we are checking that the eight address on the blockchain is same as that of the address of this instance of our adopter. Okay, so, in the first test case, we did the adoption, okay. And the address okay. In that instance of adoption, it can be referenced through this key variable and then from the blockchain we are getting the adopter number eight okay and then it is okay should be the same as this okay.

So this test cases for this assertion is for testing that scenario okay. And the third test cases if you remember in our adoption we had one method to get all the adapters right. So, here we are just checking that all the adapters we get for it okay same as this particular instance okay so again we assign that is to expect it using To discover and then get adopters okay. So, it returns us an array of 16 and then we are taking the eight address in that area is same as this instance okay. So, these three test cases cover these three different scenarios All right. Now we are going to test this.

So, again for testing purpose you need to go to the terminal here truffle test, so, it will go to the test directory and it will run all the test cases. So, now you see it is compiling just one option and now it is showing the three test cases we had and all the three test cases passed. Okay. So this is the testing framework which truffles provide you on through it, you can do unit testing for your contract on the blockchain. So this concludes our session on the second session, which covered in unit testing.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.