solidity[57]-test-deploy-contract

测试合约部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const ganache = require('ganache-cli');
const Web3 = require('web3');
//通过提供provider得到web3实例
const web3 = new Web3(ganache.provider());
//得到编译后得到的二进制文件与ABI接口。
const {bytecode,interface} = require('../compile');
//导入assert判断
const assert = require('assert');

var helloworld;
var fetchAccounts;
beforeEach( async()=>{
//得到账号
fetchAccounts = await web3.eth.getAccounts();

//部署合约斌拿到合约实例 JSON.parse(interface) 将interface转换为JSON对象。deploy函数传递要部署的二进制文件以及构造参数。
//send函数代表真正的部署,需要传递部署的账号以及gaslimit限制。
helloworld = await new web3.eth.Contract(JSON.parse(interface)).deploy({data:bytecode,arguments:['jonson']}).send({from:fetchAccounts[0],gas:'1000000'});

});
//获取到的helloworld实例是一个js对象,helloworld.options.address代表合约部署到以太坊上的地址。
因此只要存在就意味着部署成功。繁殖部署失败
describe('HelloWorld',()=>{

it('deploy contract',()=>{
// console.log(helloworld);
assert.ok(helloworld.options.address);
})
})

测试合约交互——调用静态方法

1
2
3
4
it('call static function',async ()=>{
const message = await helloworld.methods.getName().call();
assert.equal('jonson',message);
})

测试合约交互——调用动态方法

1
2
3
4
5
it('call dynamic function',async ()=>{
await helloworld.methods.changeName('olaya').send({from:fetchAccounts[0]});
const message = await helloworld.methods.getName().call();
assert.equal('olaya',message);
})

完整代码

HelloWorld.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
pragma solidity ^0.4.23;

contract HelloWorld{
string public name;


constructor(string _name) public {
name = _name;
}

function getName() public view returns(string){
return name;
}
function changeName(string _name) public{
name = _name;
}


}

compile.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const path = require('path');
const fs = require('fs');

const solc = require('solc');


const filepath = path.resolve(__dirname,'contracts','HelloWorld.sol');

console.log(filepath);

const source = fs.readFileSync(filepath,"utf8");

console.log(source);

console.log(solc.compile(source,1).contracts[':HelloWorld']);
module.exports = solc.compile(source,1).contracts[':HelloWorld'];

helloworld.test.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const ganache = require('ganache-cli');

const Web3 = require('web3');

const web3 = new Web3(ganache.provider());
// const helloworld = require('../compile');//helloworld.bytecode helloworld.interface
const {bytecode,interface} = require('../compile');
const assert = require('assert');

var helloworld;
var fetchAccounts;
beforeEach( async()=>{
fetchAccounts = await web3.eth.getAccounts();
helloworld = await new web3.eth.Contract(JSON.parse(interface)).deploy({data:bytecode,arguments:['jonson']}).send({from:fetchAccounts[0],gas:'1000000'});
console.log(fetchAccounts);
//promise
});

describe('HelloWorld',()=>{

it('deploy contract',()=>{
// console.log(helloworld);
assert.ok(helloworld.options.address);
})

it('call static function',async ()=>{
const message = await helloworld.methods.getName().call();
assert.equal('jonson',message);
})

it('call dynamic function',async ()=>{
await helloworld.methods.changeName('olaya').send({from:fetchAccounts[0]});
const message = await helloworld.methods.getName().call();
assert.equal('olaya',message);
})


})

测试

1
> npm test

返回:

1
2
3
4
5
6
7
> myproject@1.0.0 test /Users/jackson/Desktop/myproject
> mocha
HelloWorld
✓ deploy contract
✓ call static function
✓ call dynamic function (67ms)
3 passing (348ms)