Truffle-Test

幫助一個菜鳥測試:它“應該存入 1 個乙太幣”

  • January 15, 2021

我想恢復或重構這個舊的智能合約,作為學習 Solidity 和 js 編碼的練習。Truffle 編譯並遷移它,它通過了第一次測試(雖然我不確定它是否真的測試過)。無論如何,第二次測試失敗了,所以我想知道我是否首先正確設置了測試環境。這是契約和測試文件:

// warrior minimum breeze raven garden express solar flavor obvious twenty alpha actress
contract AuthorDonationExample {



 // Accounts for each of the two participants in the contract with the 'address' type.
 address payable author;
 address payable eachditor;

 // This method will be called from the Dapp.
 // Any function which uses funds must contain the keyword payable.

 function donate() public payable {
   // If no Ether has been sent we have nothing to do.
   require (msg.value != 0);

   // Do some arithmetic for an 80/20 split between Author and Editor.
   // Use a 256 bit unsigned integer that represents the value in wei
   uint editorAmount = msg.value / 5;
   uint authorAmount = msg.value - editorAmount;

   // Attempt to forward Ether to the Author and then the Editor.
   // Throw an exception if transfer fails. Ether is returned to sender.
   require (author.send(authorAmount)) ;
   require (editor.send(editorAmount)) ;
 }
}

TestAuthorDonation.js

   const AuthorDonationExample = artifacts.require("AuthorDonationExample")

contract("AuthorDonationExample", accounts => {
   var authorDonation;

   it("Should retrive deployed contract.", () =>
       AuthorDonationExample.deployed().then(function(instance){
           authorDonation = instance
     console.log('author donation', authorDonation)
     assert.isOk(authorDonation)
     // Tell Mocha move on to the next sequential test.

 }))
    // Call the donate method on the contract. Since that method is tagged payable,
   // we can send Ether by passing an object containing from, to and amount.
   // All transactions are carried sent in wei. We use a web3 utility to convert from Ether.
   authorDonation.donate({from:accounts[3], to:authorDonation.address, value: web3.utils.toWei("1", "ether")})//
   .then(function(tx) {
     // Pass the test if we have a transaction reciept returned.
     assert.isOk(tx.receipt)
     // For convenience, show the balances of accounts after transaction.
     printBalances(accounts)
     done()
   }, function(error) {
       // Force an error if callback fails.
       assert.equal(true, false)
       console.error(error)
       done()
     })
 })

   const AuthorDonationExample = artifacts.require("AuthorDonationExample")

contract("AuthorDonationExample", accounts => {
   var authorDonation;

   it("Should retrive deployed contract.", () =>
       AuthorDonationExample.deployed().then(function(instance){
           authorDonation = instance
     console.log('author donation', authorDonation)
     assert.isOk(authorDonation)
     // Tell Mocha move on to the next sequential test.

 }))
    // Call the donate method on the contract. Since that method is tagged payable,
   // we can send Ether by passing an object containing from, to and amount.
   // All transactions are carried sent in wei. We use a web3 utility to convert from Ether.
   authorDonation.donate({from:accounts[3], to:authorDonation.address, value: web3.utils.toWei("1", "ether")})//
   .then(function(tx) {
     // Pass the test if we have a transaction reciept returned.
     assert.isOk(tx.receipt)
     // For convenience, show the balances of accounts after transaction.
     printBalances(accounts)
     done()
   }, function(error) {
       // Force an error if callback fails.
       assert.equal(true, false)
       console.error(error)
       done()
     })
 })

松露測試>無法讀取未定義的屬性“捐贈”

所以這告訴你這authorDonation是未定義的。您必須AuthorDonationExample.deployed()在每個it塊中執行,否則您可以beforeEach使用它。

看來您已經刪除了第二個it塊,所以先把它帶回來,因為您有兩個單獨的斷言。

試試這個:

const AuthorDonationExample = artifacts.require("AuthorDonationExample");

contract("AuthorDonationExample", function(accounts) {
   var authorDonation;

   before(async function() {
       authorDonation = await AuthorDonationExample.deployed();
       console.log("author donation", authorDonation);
   });

   it("Should retrive deployed contract.", async function() {
       assert.isOk(authorDonation);
   });

   it("Should call donate and return a receipt.", async function() {
       // Call the donate method on the contract. Since that method is tagged payable,
       // we can send Ether by passing an object containing from, to and amount.
       // All transactions are carried sent in wei. We use a web3 utility to convert from Ether.
       const tx = await authorDonation.donate({from: accounts[3], to: authorDonation.address, value: web3.utils.toWei("1", "ether")});
       // Pass the test if we have a transaction receipt returned.
       assert.isOk(tx.receipt);
       // For convenience, show the balances of accounts after transaction.
       await printBalances(accounts);
   });
});

注意:如果printBalances不是async function,那麼我建議您這樣做…

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