-1

I wanted to revive or refactor this old smart contract as an exercise to learn solidity and js coding. Truffle compiled it and migrated it and it passed the first test (Although I'm not sure if it really did test it). Anyway, the second test failed miserably so I want to know if I set up the testing environment correctly in the first place. here are the contract and the test file:

// 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()
      })
  })

Truffle-test> Cannot read property 'donate' of undefined

  • Please revise the code of your files. Knowing what project are you trying to refactor: 1) "eachditor" variable should be "editor". 2) editor and author accounts value are 0x0 in your contract... 3) You've mixed the originals ìt of the test in a new duplicated one... – alberto Nov 5 '19 at 0:49
0

So this tells you that authorDonation is undefined. You have to either run AuthorDonationExample.deployed() in each it block, otherwise you can do a beforeEach with that.

It also seems you've removed the second it block, so bring that back first because you have two separate assertions.

| improve this answer | |
  • Sorry my question wasnt organized but I did include it in the contract and ran a test: > Cannot read property 'donate' of undefined – Basém Alasi Nov 4 '19 at 23:42
  • Please see edited answer. – Kaden Zipfel Nov 5 '19 at 0:15
0

Try this:

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);
    });
});

Note: if printBalances is not an async function, then I recommend that you make it so...

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.