blockchainethereumsoliditytruffleconsensys-truffle

Having a Problem Testing my Solidity Smart Contract (w/ Truffle)


So Im building a basic NF Token. Created a basic minting function and mapping. Im testing the app with truffle, tried both solidity testing and JS testing. Having the following errors.

Using network 'development'.

Compiling ./contracts/NFCertificate.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/AddressUtils.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/introspection/ERC165.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/introspection/SupportsInterfaceWithLookup.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/math/SafeMath.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/ownership/Ownable.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC721/ERC721Basic.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC721/ERC721BasicToken.sol...
Compiling ./node_modules/openzeppelin-solidity/contracts/token/ERC721/ERC721Receiver.sol...
Compiling ./test/TestCertificate.sol...
Compiling truffle/Assert.sol...
Compiling truffle/DeployedAddresses.sol...

Compilation warnings encountered:

/Users/aditya/Desktop/Work & Hobbies/Ideas/Blockchain/Blockchain Development/Ethereum:dApp/CertificateContract/contracts/NFCertificate.sol:26:35: Warning: This function only accepts a single "bytes" argument. Please use "abi.encodePacked(...)" or a similar function to encode the data.
        uint256 tokenId = uint256(keccak256(certificateNum, msg.sender, title, message));
                                  ^---------------------------------------------------^



  TestCertificate
    1) testNumber
    > No events were emitted

  Contract: NFCertificate
    2) It should return the same number
    > No events were emitted


  0 passing (1s)
  2 failing

  1) TestCertificate
       testNumber:
     Error: VM Exception while processing transaction: revert
      at Object.InvalidResponse (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/errors.js:38:1)
      at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/requestmanager.js:86:1
      at /usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-provider/wrapper.js:134:1
      at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/httpprovider.js:128:1)
      at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:64:1)
      at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:354:1)
      at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:509:1)
      at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:469:1)
      at endReadableNT (_stream_readable.js:1081:12)
      at process._tickCallback (internal/process/next_tick.js:63:19)

  2) Contract: NFCertificate
       It should return the same number:
     TypeError: instance.returnNumbers is not a function
      at Context.<anonymous> (test/TestCertificate.js:9:31)
      at /usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-core/lib/testing/testrunner.js:135:1
      at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/property.js:119:1
      at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/requestmanager.js:89:1
      at /usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-provider/wrapper.js:134:1
      at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/httpprovider.js:128:1)
      at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:64:1)
      at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:354:1)
      at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:509:1)
      at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:469:1)
      at endReadableNT (_stream_readable.js:1081:12)
      at process._tickCallback (internal/process/next_tick.js:63:19)

The test scripts are running a simple internal function in the contract that returns an int = 1000 and compares that to the expected variable (let expected = 1000) declared in the test. Heres the JS test script

  import assertRevert from "zeppelin- 
  solidity/test/helpers/assertRevert";

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

  contract("NFCertificate", () => {
      it("It should return the same number", function() {
          let instance = NFCertificate.deployed();
          let expected = 1000;
          assert.equal(instance.returnNumber(), expected);
      });
  });

Heres the Solidity Test Script:

pragma solidity ^0.4.20;

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

contract TestCertificate {

  function testNumber() public {
    NFCertificate cert = NFCertificate(DeployedAddresses.NFCertificate());
    uint expected = 1000;
    Assert.equal(cert.returnNumber(), expected, "Numbers should be equal");
  }
}

I also tried testing minted token ID's, and also owners of tokens based on the mapping I declared, Im getting the same problem. When writing a test contract in javascript it can't recognize the functions in the original NFT contract. When writing a test contract in solidity it almost always says "Error: VM Exception while processing transaction: revert" and nothing else.

Finally here's the contract im trying to test out. Any and all help is appreciated, im very new to coding and ethereum so I've probably made numerous mistakes.

pragma solidity ^0.4.24;

import '../node_modules/openzeppelin-solidity/contracts/token/ERC721/ERC721BasicToken.sol';
import '../node_modules/openzeppelin-solidity/contracts/ownership/Ownable.sol';

contract NFCertificate is ERC721BasicToken, Ownable {
    struct Certificate {
        uint certNum;
        uint256 tokenId;
        bytes32 title;
        bytes32 message;
        address owner;
    }

    mapping (uint256 => address) tokenToOwner;
    mapping (address => uint256) ownerToToken;
    mapping (uint256 => string) tokenIdToName;

    event returnNumbers(uint number);

    Certificate[] public certificates;

    function createCert(bytes32 title, bytes32 message) public returns (bytes32){
        uint certificateNum = certificates.length - 1; 
        uint256 tokenId = uint256(keccak256(certificateNum, msg.sender, title, message));
        certificates.push(Certificate(certificateNum++, tokenId, title, message, msg.sender));
        tokenToOwner[tokenId] = msg.sender;
        ownerToToken[msg.sender] = tokenId;
        _mint(msg.sender, tokenId);
    }

    function returnNumber() public returns(uint) {
        uint number = 1000;
        returnNumbers(number);
        return number;
    }

    function whatTokensDoYouOwn(address owner) public view returns(uint256) {
        return ownerToToken[owner]; 
    }


}

Solution

  • NFCertificate.deployed() returns a promise, as does instance.returnNumber(). So the JS should be:

    contract("NFCertificate", () => {
      it("It should return the same number", async function() {
          let instance = await NFCertificate.deployed();
          let expected = 1000;
          assert.equal(await instance.returnNumber(), expected);
      });
    });
    

    As returnNumbers is an event, it should be emitted using the emit keyword. It's also good to style events to start with a capital, otherwise it might look like a function. So event ReturnNumbers(uint number);

    function returnNumber() public returns(uint) {
        uint number = 1000;
        emit ReturnNumbers(number);
        return number;
    }
    

    Adding the async/await lines should fix your JavaScript tests. If you later want to assert that your ReturnNumbers events were emitted correctly, I would suggest to use my truffle-assertions library, which includes functions to assert that events have or have not been emitted. It also includes functions to assert reverts and other failures in a straightforward way.