4)単体TEST
この段階で、Remix を使ってTESTもできるが
ちゃんと test.js を使ってTESTする
**TEST仕様書のもとにもなる
先ずは必要なものを npm install する
1 |
npm install --save ganache-cli mocha fs-extra web3@1.0.0-beta.26 |
ganache-cli:単体TEST用の network node:
mocha:javascriptのTESTモジュール
fs-extra:FileSystem モジュール
web3@1.0.0-beta.26:javascriptを動かすモジュール
Webでdappを動かす時も使う
1.0.0-beta.26 を使うのは非同期処理を簡単にするため
** asinc , await を使う
solc:solidity Compiler はtruffle組み込みのものでcompile済なのでここでは不要
cd ./test に hoge.js を書いていく
File名は特に制約はない
./test/**.js と ./test/ ディレクトリに置くことがPOINT
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 |
const assert = require('assert'); const ganache = require('ganache-cli'); const Web3 = require('web3'); const provider = ganache.provider(); const web3 = new Web3(provider); const compiledMyProject =require('/home/aoki/truffle/myproject/build/contracts/MyProject.json'); let myproject; let accounts; beforeEach(async ()=>{ accounts = await web3.eth.getAccounts(); myproject = await new web3.eth.Contract(compiledMyProject.abi) .deploy({data: compiledMyProject.bytecode}) .send({ from: accounts[0], gas: 5000000}); }); describe('myproject Contract', () => { it('deploys a contract',() =>{ console.log(accounts); assert.ok(myproject.options.address); }); it('Initial message の確認',async() =>{ const msg = await myproject.methods.message().call({ from:accounts[0] }); assert.equal(msg,'Initial Message','STEP01'); }); }); |