1

I have the following function:

pragma solidity ^0.4.18; 

contract PromoContract {
      uint256 public promoCreatedCount;

      function createPromoPerson(address _owner, string _name, uint256 _price) public onlyCOO {
        require(promoCreatedCount < 5000);

        address personOwner = _owner;
        if (personOwner == address(0)) {
          personOwner = cooAddress;
        }

        if (_price <= 0) {
          _price = startingPrice;
        }

        promoCreatedCount++;
        _createPerson(_name, personOwner, _price);
      }
}

I started the following Test Contract:

pragma solidity ^0.4.18;

import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/PromoContract .sol";

contract TestCryptoSportsToken {
    PromoContract promoContract = PromoContract (DeployedAddresses.PromoContract ());
    //check for events

    // Testing the adopt() function
    function testCreatePromoPerson() public {
      promoContract.createPromoPerson("0xf7ca47eabf0e98af942fa45a5e8cdc05b7100eb4", "Test Person", 1);

//check for ?
    }
}

Any suggestion how to test the contract function createPromoPerson. I appreciate your replies!

1

You can just check the first event from logs which you get in your tx receipt:

it("should show the transfer event", function() {
  var token;
  return Token.deployed().then(function(instance){
    token = instance;
    return token.transfer(accounts[1], 100000);
  }).then(function(result){
    console.log(result.logs[0].event)
  })
});

This particular test will print to console the name of the triggered event and pass the test.


Code from Smart Contract Unit Testing


Another way will be to just check the length of logs:

assert.equal(result.logs.length, 1, "No events were triggered.");
  • Thx for your fast reply! Would you be so kind to write your code in solidity, cause I think it might be extremely interesting to see this testing on solidity code? – Soli Feb 2 '18 at 21:22
  • @Kare you can't see events in Solidity. They are only accessible in the logs which are part of the transaction. Hence you have to check if the events were triggered in js (or in any other language you are writing tests). – Roman Frolov Feb 2 '18 at 21:25

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.