January 25, 2026

Capitalizations Index – B ∞/21M

Truffle + Mocha テスティング技法 – Yuya Sugano –

Truffle + mocha テスティング技法 – yuya sugano –

Truffle + Mocha テスティング技法 – Yuya Sugano –

Truffle + mocha テスティング技法 – yuya sugano –

そこで revert を補足する関数を utils.js へ追加します。例外に revert が含まれている場合は trueを返し、そうでない場合は false を返してくれる関数です。

isEVMEXception(err) ,

revert による例外の場合は catch 句内のトークン残高の確認へ進みます。仮に 10000 MetaCoin 送金できてしまった場合は Bob の残高がおかしいので assert(false, '10000 was in the second account') とします。

it("should fail if the user does not have enough MetaCoin", async () => );
} catch (err)
assert(false, '10000 was in the second account');
});

テストを実行します。

$ truffle test
Using network 'test'.
Compiling ./contracts/ConvertLib.sol...
Compiling ./contracts/MetaCoin.sol...
Compiling ./test/TestMetacoin.sol...
Compiling truffle/Assert.sol...
Compiling truffle/DeployedAddresses.sol...
TestMetacoin
? testInitialBalanceUsingDeployedContract (129ms)
? testInitialBalanceWithNewMetaCoin (113ms)
Contract: MetaCoin
Token
? should put 10000 MetaCoin in the first account
? should fail if the user does not have enough MetaCoin
? should send coin correctly (323ms)
Library
? should call a function that depends on a linked library (62ms)
6 passing (2s)

上記のテスト内容を文章に書き出してみました。いずれも文章として何をテストしているかが理解できると思います。

  • Contract: MetaCoin Token should put 10000 MetaCoin in the first account
  • Contract: MetaCoin Token should fail if the user does not have enough MetaCoin
  • Contract: MetaCoin Token sendCoin should send coin correctly
  • Contract: MetaCoin Library should call a function that depends on a linked library

リファクタしたコードです。

最後に本題のTDD(テスト駆動開発)です。以下の 『MetaCoin の受信者が address(0) でないことを確認するコード』をコメントアウトして、この部分に対応する『失敗するテスト』を書いてみます。

function sendCoin(address receiver, uint amount) public returns(bool sufficient) );
} catch (err)
assert(false, 'sendCoin sent coin to zero address');
});

sendCoin(receiver, amount, )revert されないため、最後のアサーションエラーが発生します。このテストを実行すると sendCoin sent coin to zero address が出力されます。

1) Contract: MetaCoin
Token
should not send coin to zero address:
AssertionError: sendCoin sent coin to zero address
at Context.it (test/metacoin.js:60:7)
at processTicksAndRejections (internal/process/next_tick.js:81:5)

次にテストをパスするコードを記述します。ここではコメントアウトした行のコメントを元に戻すだけです。最終テストの結果です。address(0) に MetaCoin が送付できないことが確認できました。

$ truffle test
Using network 'test'.
Compiling ./contracts/ConvertLib.sol...
Compiling ./contracts/MetaCoin.sol...
Compiling ./test/TestMetacoin.sol...
Compiling truffle/Assert.sol...
Compiling truffle/DeployedAddresses.sol...
TestMetacoin
? testInitialBalanceUsingDeployedContract (161ms)
? testInitialBalanceWithNewMetaCoin (126ms)
Contract: MetaCoin
Token
? should put 10000 MetaCoin in the first account
? should fail if the user does not have enough MetaCoin (58ms)
? should send coin correctly (164ms)
? should not send coin to zero address (68ms)
Library
? should call a function that depends on a linked library (61ms)
7 passing (2s)

リファクタしたコードです。

TDD(テスト駆動開発)についてはMocha/Chaiで十分だと考えますが、BDD(振る舞い駆動開発)についてはRSpecやCucumberなどのフレームワークが便利です。truffleのテスティングにもBDDのフレームワークが追加されることを願っています。

Published at Thu, 23 May 2019 09:13:53 +0000

Previous Article

Bitcoin Pullback Begins, Will BTC Drop to $6,000 This Time?

Next Article

Coinstar Expands Bitcoin Buying Service to Cover 21 US States

You might be interested in …

Flip me review method to make money online dont miss out

flip me REVIEW method to MAKE MONEY online Dont miss out

flip me REVIEW method to MAKE MONEY online Dont miss out https://warriorplus.com/o2/a/fnrn6/0 flip me REVIEW method to MAKE MONEY online Dont miss out. The easiest way to make money online and you could be earning […]

Deadline Looms on Ethereum Classic DAO Withdrawals

Deadline Looms on Ethereum Classic DAO Withdrawals Holders of Ethereum Classic DAO tokens must withdraw by April 15th. Otherwise, their value will vanish. There are still 1,609,982 ETC remaining to be withdrawn from the public “Robin Hood” Ethereum […]

Update bei LocalBitcoins: Tschüss, Privatsphäre!

BTC-ECHO Update bei LocalBitcoins: Tschüss, Privatsphäre! Die Handelsplattform LocalBitcoins führt ab dem 25. Mai neue Regeln ein. Um künftig auf der Plattform zu handeln, muss man die Know-Your-Customer-Regeln befolgen und in bestimmten Fällen seine Identität […]