前節でInboxのコントラクトを作成しました。Remixで開発した内容をローカルで作成してwebで利用するには、ローカルでも開発できる環境にする必要があります。macの場合は「ターミナル」,windowsの場合は「コマンドプロンプト」または、「windows Power Shell(PS)」を開き進めていきます。 コマンドが異なる場合があるので、windowsはPSモードだと良いと思います。
beforeEach(() => {
//Get a list of all accounts
web3.eth.getAccounts().then(fetchedAccounts => {
console.log(fetchedAccounts);
});
//Use one of those accounts to deploy
//the contract
});
describe('Inbox', () => {
it('deploy a contract', () => {});
});
非同期処理を書く
アカウントが作られる時間が少しだけかかるため、非同期処理に変更します。
Inbox.test.js
let accounts;
beforeEach(async() => {
//Get a list of all accounts
accounts = await web3.eth.getAccounts();
});
describe('Inbox', () => {
it('deploy a contract', () => {
console.log(accounts);
});
});
Inboxのテストコードを作成する STEP1
Inbox.test.js
const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const { interface, bytecode } = require('../compile'); //追記
let accounts;
let inbox; //追記
beforeEach(async() => {
//Get a list of all accounts
accounts = await web3.eth.getAccounts();
//Use one of those accounts to deploy
//the contract 以下追記
inbox = await new web3.eth.Contract(JSON.parse(interface))
.deploy({ data: bytecode, arguments: ['Hi there!'] })
.send({ from: accounts[0],gas:'1000000' });
});
describe('Inbox', () => {
it('deploy a contract', () => {
console.log(inbox); //追記
});
});