Truffle Testing – Simulating Time Passage with TestRPC

testrpctruffle

Any tips on how to simulate real network conditions on the testrpc testing client using truffle? I have some functions that look at blocknumber and try to deal with passing time. Has anyone come up with a good way to test these kind of things?

I'm wondering if just calling some null transactions in a loop with increase the block number accordingly. Any info on the inner workings of these things would be appreciated.

Best Answer

By default, every transaction called in testrpc is immediately mined and the block height will increase. The time it takes to mine each block can be set as a command line option when starting testrpc using -b or --blocktime

In addition, testrpc provides a custom rpc method you can use to manipulate the block height:

evm_mine : Force a block to be mined. Takes no parameters. Mines a block independent of whether or not mining is started or stopped.

Testrpc's custom methods can be called from web3 using web3.currentProvider.sendAsync() like so:

web3.currentProvider.sendAsync({
  jsonrpc: "2.0",
  method: "evm_mine",
  id: 12345
}, function(err, result) {
  // this is your callback
});
Related Topic