How to ensure correct ordering when using a for loop in truffle test
How to ensure correct ordering when using a for loop in truffle test
Loading saved threads...
EthJ · External communityPost link
External question — Ethereum Stack Exchange
Author: EthJ
Original post: https://ethereum.stackexchange.com/questions/12773
License: CC BY-SA 3.0 — https://creativecommons.org/licenses/by-sa/3.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I am using truffle (javascript) to test my solidity contracts and want to automatically run my contract functions a large number of times. I can't figure out how to make a for loop work with promises however as I need it to wait until the whole loop is finished to start again. I'm currently just using a regular for loop with the promises chained up within it. I have included below my roughly anonymised code.
Thanks
for(var j = 0; j < 5; j++) {
//create parameters for function
runFunc(param1 ,param2);
}
function runFunc(param1, param2) {
contract.func1.call(param1).then(function(return1) {
var var1 = = web3.toAscii(account1hash.valueOf());
contract.func2(param2, var1).then(function() {
contract.func3(param1, param2);
});
});
}
Quote
Report
Abdul Khan · External communityPost link
External answer — Ethereum Stack Exchange
Author: Abdul Khan
Original post: https://ethereum.stackexchange.com/a/103528
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
you can do something like this...
NB: accounts is a global variable with test wallets passed in to the test by truffle.
should.be.fulfilled is using the chai-as-promised NPM module - checks for errors
it('Checks we are keeping track of total enlisted', async function() {
for (var i=0; i < accounts.length; i++) {
await this.WhitelistV2.enlist(accounts[i], {from: _}).should.be.fulfilled;
total +=1;
}
total.should.be.equal(accounts.length);
});
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Ethereum Stack Exchange Author: Abdul Khan Source score (net votes, not local likes): 0 Original post: https://ethereum.stackexchange.com/a/103528 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. you can do something like this... NB: accounts is a global variable with test wallets passed in to the test by truffle. should.be.fulfilled is using the chai-as-promised NPM module - checks for errors it('Checks we are keeping track of total enlisted', async function() { for (var i=0; i < accounts.length; i++) { await this.WhitelistV2.enlist(accounts[i], {from: _}).should.be.fulfilled; total +=1; } total.should.be.equal(accounts.length); });
Checking account access…