dimanche 7 octobre 2018

how to create new ethereum/solidity contract for each test in javascript/truffle

background

I have written an ethereum smart-contract in the Solidity language. In order to test things, I can run a local node using Ganache and deploy my contract on it using truffle migrate.

requirements

I want to test my contract using JavaScript. I want to create a new instance of my contract for each test.

what i've tried

I created a test file tests/test.js in my project:

const expect = require('chai').expect

const Round = artifacts.require('Round')


contract('pledgersLength1', async function(accounts) {
    it('1 pledger', async function() {
        let r = await Round.deployed()
        await r.pledge(5)
        let len = (await r.pledgersLength()).toNumber()
        expect(len).to.equal(0)
    })
})
contract('pledgersLength2', async function(accounts) {
    it('2 pledgers', async function() {
        let r = await Round.deployed()
        await r.pledge(5)
        let len = (await r.pledgersLength()).toNumber()
        expect(len).to.equal(1)
    })
})

I run it with truffle test. It's basically Mocha, but truffle defines artifacts for you with a JavaScript connection to the smart contracts.

The truffle contract function is almost the same as Mocha's describe function, with a small change that I don't understand! I assumed that contract would make my contract new each time. It doesn't. Perhaps I can use something like new Round() instead of Round.deployed(), but I just don't know how.

The solution does not have to use truffle.

Aucun commentaire:

Enregistrer un commentaire