Solidity

松露、堅固或摩卡的單元測試?

  • October 22, 2018

松露允許其中之一。現在我想知道我將使用哪個來測試我的程式碼。我不是摩卡的忠實粉絲。當我看到類似的程式碼時

contract('MetaCoin', function(accounts) {   it("should put 10000 MetaCoin in the first account", function() {
   return MetaCoin.deployed().then(function(instance) {
     return instance.getBalance.call(accounts[0]);
   }).then(function(balance) {
     assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account");
   });

我認為在可讀性方面確實出了點問題。另一方面,寫一份契約來測試另一份契約的想法讓我很不舒服。使用這兩種不同方法進行單元測試有哪些注意事項?

我已經為不同的智能合約編寫了一千多個測試,並且可以說你幾乎總是希望將 javascript/mocha 與 truffle 一起使用。我遇到的情況很少,您會想要使用solidity。為了執行測試,將合約設置為您想要的狀態要容易得多。

另外……您真的不需要像給出的範例中那樣連結函式呼叫。您可以使用 async/await,這將使事情變得更加清晰和可讀。我創建了一個樣板松露項目,用於我的任何新項目。我會通過範例等對其進行更新。我強烈建議您檢查其中的測試方式…

回購:

https://github.com/TovarishFin/smart-contract-boilerplate

使用 async/await 直接連結到測試文件:

https://github.com/TovarishFin/smart-contract-boilerplate/blob/master/test/ExampleCoin.js

替換:assert.equal(balance.valueOf(), 10000, “10000不在第一個賬戶中”);

和:

assert.equal(balance.valueOf() === 10000, 1); // 如果為 true,則為 false 將 1 替換為 -1

引用自:https://ethereum.stackexchange.com/questions/26671