How to test the contract deployed with OpenZeppelin SDK?

I created and deployed the contract with Open ZeppelinSDK and I want to test with JS code.
How do I write the test code?
I used to use the truffle test but that is not reflected the Open Zeppelin architecture.
I want to test following contract.

Test.sol

pragma solidity ^0.5.11;

contract Test {

    uint public number;

    function add(uint a, uint b) public returns (uint) {
        number = a + b;
    }
}
1 Like

Hi @Shinsaku,

You can use truffle test (you will need a truffle-config.js file).

You can then test the logic contract just like you would test any other contract.

You can also test the contract as an upgradeable contract, see Testing upgradeable projects documentation for details.

Feel free to ask more questions.

For testing with truffle you need to get the truffle suite work. That means, running a test blockchain. So first run ganache or ganache-cli. This is the place you will deploy your contract with truffle test.

Since ganache and ganache-cli answer to differnt ports, you may make two entries for both in your truffle.js (Windows I think truffle-js). This file should exist and it is normaly created during install of truffle:

ganache defaults to port 7545
ganache-cli defaults to port 8545

So here are some modificaton for truffle js in your truffle project root directory:

open the file and goto module.export object. Then you may add

module.exports = {
  // See <http://truffleframework.com/docs/advanced/configuration>
  // for more about customizing your Truffle configuration!
  networks: {
    ganacheCLI: {
      host: '127.0.0.1',
      port: 8545,
      network_id: '*', // Match any network id
    },
    ganache: {
      host: '127.0.0.1',
      port: 7545,
      network_id: '*', // Match any network id
    },
  },

To test your truffle.js file. Start ganache or ganache-cli

truffle console --network ganache

or

truffle console --network ganacheCLI

If truffle console connects, your truffle.js config is fine.

So then you can goto to the test directory and add a file, for example MyFirstContract.test.js

For testing type a console.log(‘MyFirstContract is alive’);

Then truffle test -network ganache and it should work.

After that start with programming the testcode

BTW. one single tip. Avoid naming files test*, contracts or methods test. This is a good practice and avoids problems, because often test is a predefined keyword.

Hope this helps

1 Like

Thank you!
I was successful in testing upgradeable architecture contract!

1 Like