I do some tests for my application working with smart-contracts, for that I use the test-environment module provided by openzepplin. But I don’t really understand how it works, when I put it in my application I use the web3 module to configure and use my method in my contract, but with test-environment, the methods are not recognized and I cannot use them.
Details
in my app i run a method with : contract.methods.owner()
but in my test, I only can use contract.owner() directly, without .methods, but the methods of my API are working fine with the first method and can’t work without the .methods search in the contract instead of the test.
Code to reproduce
what is use for test but not working with my app:
beforeEach(async function {
this.myContract = await MyContract.new({ from: owner });
});
it('the deployer is the owner', async function () {
expect(await this.myContract.owner()).to.equal(owner);
});
What I want to do: (add .methods between the contract and the methods use like in web3)
beforeEach(async function {
this.myContract = await MyContract.new({ from: owner });
});
it('the deployer is the owner', async function () {
expect(await this.myContract.methods.owner()).to.equal(owner);
});
Error:
1) owner
deployer is owner:
TypeError: myContract.methods.owner is not a function
at Context.<anonymous> (test/contract.spec.js:22:53)
at processImmediate (internal/timers.js:456:21)
without that I can’t test my API. if someone can tell me how to work around or if I miss somethings
Hey @vincent! Thank you for trying test-environement!
There are two web3 contract abstractions: Truffle and Web3.js. By default test-environment is configured to use Truffle abstraction that is why this.myContract.owner() works. In order to switch to web3.js contract abstraction and use contract.methods.owner() you need to configure test-environment.config.js file like this:
// test-environment.config.js
module.exports = {
accounts: {
amount: 10, // Number of unlocked accounts
ether: 100, // Initial balance of unlocked accounts (in ether)
},
contracts: {
type: 'web3', // Contract abstraction to use: 'truffle' for @truffle/contract or 'web3' for web3-eth-contract
defaultGas: 6e6, // Maximum gas for contract calls (when unspecified)
// Options available since v0.1.2
defaultGasPrice: 20e9, // Gas price for contract calls (when unspecified)
artifactsDir: 'build/contracts', // Directory where contract artifacts are stored
},
blockGasLimit: 8e6, // Maximum gas per block
};
You can learn more here. Let me know how it goes for you!
Thank you for your clear and constructive response. After your reading and various tests, I understood the operation and the configuration to set up to use your module. Thank you for your help. For the sake of clarity I put here the code that I wrote so that it works: