Hot answers tagged

18

All it needed was to add a package.json file to the project directory with some babel dependencies and doing an 'npm install'. Also, adding a '.babelrc' file to the truffle project directory. Finally, adding some requires to truffle.js file. package.json file { "name": "game-token", "devDependencies": { "babel-preset-es2015": "^6.18.0", "babel-...


6

I tried i_robot's solution, and it almost* worked, I had to add: require('babel-register')({ ignore: /node_modules\/(?!zeppelin-solidity)/ }); require('babel-polyfill'); at the top of my truffle.js. The ignore field is important!! In addition to adding babel-polyfill in my package.json dependencies: "dependencies": { "babel-polyfill": "^6.26.0", ...


6

Add this in your Truffle configuration file (truffle.js or truffle-config.js): mocha: { enableTimeouts: false, before_timeout: 120000 // Here is 2min but can be whatever timeout is suitable for you. } If you want to preserve timeouts (and even configure different timeouts for different tests), then simply follow the instructions here.


5

Yes that is the case. accounts[0] is the default. If you like to test to call a function form an another account you can pass {from: accounts[1]} as the last parameter of your function. If you would have a function like this: MyContract.getBalance(address); It would look like this: MyContract.getBalance.call(address, {from: accounts[1]}); You can get ...


5

Explanation: This functionality is not available in Truffle at the moment, as logged events are not displayed on passing tests (source code). I like the idea of tracking events on passing tests as well, and it seems Truffle will eventually have this feature (tracking issue). Workaround: One way to show the events is to simply watch them as per the web3 ...


5

Add the following to your configurations in launch.json: "configurations": [ { "name": "run tests", "type": "node", "request": "launch", "program": "${workspaceRoot}/node_modules/truffle/build/cli.bundled.js", "args": ["test"], "cwd": "${workspaceRoot}", "outFiles": [...


4

The error message "n: Exceeds block gas limit" means your transaction has declared a gas value greater that the maximum allowed in the network. A client will reject such transaction. With ganache-cli you can launch with a larger amount of gas available with the -l parameter: ganache-cli -l 8000000 Will launch ganache with a block gas limit of 8M. ...


4

I have written more than a thousand tests for different smart contracts and can say that you will almost always want to use javascript/mocha with truffle. There are very few cases that I have run into where you will want to use solidity instead. It is just so much easier to setup your contracts to the state that you want in order to run the tests. Also... ...


3

Maintainer of ganache here. This is a known bug in the beta. See https://github.com/trufflesuite/ganache-cli/issues/471


3

I can do it by adding this to the Truffle configuration file (truffle.js or truffle-config.js): mocha: { useColors: false, reporter: "json" } Available reporters are described here.


2

Turned out that the issue was with the way I had deployed the contract using Truffle. Basically Proxy contract's constructor took the address of ERCToken as an input. So Truffle's deployed method resolved correctly only for the main contract (ERCToken) and never for Proxy. I had to refactor the constructor code by adding separate function to set the ...


2

You're quite right to be baffled - the behaviour you observe is wrong. This is an open issue with testrpc, and as you say it is related to the calling of a library function: https://github.com/ethereumjs/testrpc/issues/122 Sorry I couldn't be of more help - you can keep an eye out on the issue for when it is fixed officially!


2

You can use .valueOf() to get the return value from the getter function. I've used this in one of the test cases I wrote sometime back. You can take a look at this for reference. Hope this helps.


2

OK, I finally debugged it. In my tests I was using Token.address syntax to get an address when I should have been using my tokenInstance.address variable that returns on Token.deployed(). Thanks!


2

I have read the solution in one of Stephen Grider's Udemy course: const assert = require('assert'); const ganache = require('ganache-cli'); const Web3 = require('web3'); const provider = ganache.provider(); const web3 = new Web3(provider); const { interface, bytecode } = require('../compile'); let lottery; let accounts; ...


2

After debugging around I've visited ganache-core repo and found the solution inside their test folder. The file is ganache-core/test/runtime_errors.js and the solution to my problem is the code below: ... testState.ErrorContract.deploy({data: testState.code}) .send({from: testState.accounts[0], gas: 3141592}) .then(function(instance) { /...


2

Had the same problem in the past, did you install node, mocha or web3 as root? Installing as root, will not let you use them as you want to do here because as you install as root, the files get installed out of the project folder with different permissions than usual applications. So to solve that: Erease the node_modules folder Re-install all the packages:...


2

Thanks to Ismael's solution: const ganache = require('ganache-cli'); const Web3 = require('web3'); const options = { gasLimit: 8000000 }; const provider = ganache.provider(options); // quote from doc "Both .provider() and .server() take a single object // which allows you to specify behavior of ganache-cli" // https://github.com/trufflesuite/ganache-cli#...


2

In function enter you require msg.value > 0.1 ether, but then you call it with 0.02 ether. So the test does exactly what it's supposed to do - revert this function call.


2

Even thought they have similarly confusing names, they refer to different things. The format { key: value } is web3 format for providing extra meta data for the function. Those are not passed on as parameters for the function but are handled internally. For example the key from can be used to define from which of the available accounts the transaction is ...


2

You can use the "before" "beforeEach" "after" and "afterEach" methods to run certain things you need before / after tests. You can also nest "describe" blocks to a max of 4 levels ( if i remember correctly ), which can be used to describe the current state you are testing. The idea is to use before blocks for things you want to affect only once, and ...


2

It's rather a JS question, but anyway, here the answer, since it's useful to know when writing Ethereum tests in JS: The problem with your second code fragment is that your test case is considered as done before your "then"-part is executed. I would recommend to try not to mix async/await and then as far as possible. Variant 1 (no async function, return ...


1

Because you're catching the error in your test. The idea in Mocha is that exceptions are caught at the framework level, not at the "application" level. A test fails if it throws an exception to the Mocha framework, and passes otherwise.


1

You can confirm that the bytecode has been zeroed out. Use the web3.eth.getCode(yourContractAddress) explained here. before selfdestruct, you get the proper code, like 0x6040... after selfdestruct, you get 0x Of course, as mentioned by Ignacio and Badr, you can, and should, also test that the side actions have been executed correctly: the expected event ...


1

I assume Mocha creates a testRPC instance somehow... No, it is you who needs to run Ganache (formerly known as TestRPC) before you run your Mocha test. In this common setup, the Ganache process runs as a server, and your Mocha test runs as a client. The protocol of communication between them is typically Web3 (over HTTP or WebSocket). Here is an example ...


1

Yes, the reason is your contract is too complex. You can obviously change the gas limit of block in test env. But it is never recommended to use such complex contracts. You can better break the contract into parts and deploy each contract separately.


1

When making tests with truffle test you can request for deploying contract in the testfile Here is an example of a series of tests i wrote to give an example : var Organizations = artifacts.require("./Organizations.sol"); // Deploy an instance of the contract contract('Organizations',function(accounts){ it("Owner on deployment should be : " + accounts[...


1

To skip blocks create a simple contract that has a function that requires a transaction. pragma solidity ^0.4.0; // file: BlockMinder.sol // used to "waste" blocks for truffle tests contract BlockMiner { uint blocksMined; function BlockMiner(){ blocksMined = 0; } function mine() { blocksMined += 1; } } After deploying ...


1

As the Truffle documentation explains: The contract() function provides a list of available accounts as a second parameter with which you can write tests against. You don't have to config the accounts params. If you are testing the contract in testrpc, the accounts passed will be those that got generated in testrpc.


1

I think the format your looking for is: service.createTicket(arg1, arg2, {value: 30, from: accounts[3]}, function(err, result) { console.log(result) } At least, that works for me.


Only top voted, non community-wiki answers of a minimum length are eligible