question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Looks like function overrides are not supported by test framework

See original GitHub issue
  • I’ve asked for help in the Truffle Gitter before filing this issue.

Issue

If a contract has an override function, unit tests for that function break.

Steps to Reproduce

Simplest example I could create to reproduce the issue

Contract:

pragma solidity ^0.4.18;

contract BaseExample {
    function approve(address _spender, uint _value) public returns (bool) {
        return true;
    }

    function increaseApproval(address _spender, uint _addedValue) public returns (bool) {
        return true;
    }
}

contract OverrideExample is BaseExample {

    function approve(address _spender, uint _value, bytes _data) public returns (bool) {
        return true;
    }

    function increaseApproval(address _spender, uint _addedValue, bytes _data) public returns (bool) {
        return true;
    }
}

Unit test:

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

contract("OverrideExample", (accounts) => {
    let instance;

    before(async () => {
        instance = await OverrideExample.deployed();
    })

    it("Test lowercase method - base", async () => {
        await instance.approve(0x0, 1);
    });

    it("Test lowercase method - override", async () => {
        await instance.approve(0x0, 1, 0x0); // BREAKS
    });

    it("Test camelCase method - base", async () => {
        await instance.increaseApproval(0x0, 1); // BREAKS
    });

    it("Test camelCase method - override", async () => {
        await instance.increaseApproval(0x0, 1, 0x0);
    });
})

Run truffle migrate and then truffle test.

Expected Behavior

No errors, all tests pass

Actual Results

When I run truffle migrate and then truffle test, I get the following error:

1) Contract: OverrideExample Test lowercase method - override:
     Error: Invalid number of arguments to Solidity function
      at Object.InvalidNumberOfSolidityArgs (/Users/leyboan1/.local/share/npm/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/errors.js:25:1)
      at SolidityFunction.validateArgs (/Users/leyboan1/.local/share/npm/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/function.js:74:1)
      at SolidityFunction.toPayload (/Users/leyboan1/.local/share/npm/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/function.js:90:1)
      at SolidityFunction.sendTransaction (/Users/leyboan1/.local/share/npm/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/function.js:163:1)
      at SolidityFunction.execute (/Users/leyboan1/.local/share/npm/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/function.js:256:1)
      at /Users/leyboan1/.local/share/npm/lib/node_modules/truffle/build/webpack:/~/truffle-contract/contract.js:202:1
      at /Users/leyboan1/.local/share/npm/lib/node_modules/truffle/build/webpack:/~/truffle-contract/contract.js:155:1
      at process._tickCallback (internal/process/next_tick.js:109:7)

  2) Contract: OverrideExample Test camelCase method - base:
     Error: Invalid number of arguments to Solidity function
      at Object.InvalidNumberOfSolidityArgs (/Users/leyboan1/.local/share/npm/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/errors.js:25:1)
      at SolidityFunction.validateArgs (/Users/leyboan1/.local/share/npm/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/function.js:74:1)
      at SolidityFunction.toPayload (/Users/leyboan1/.local/share/npm/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/function.js:90:1)
      at SolidityFunction.sendTransaction (/Users/leyboan1/.local/share/npm/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/function.js:163:1)
      at SolidityFunction.execute (/Users/leyboan1/.local/share/npm/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/function.js:256:1)
      at /Users/leyboan1/.local/share/npm/lib/node_modules/truffle/build/webpack:/~/truffle-contract/contract.js:202:1
      at /Users/leyboan1/.local/share/npm/lib/node_modules/truffle/build/webpack:/~/truffle-contract/contract.js:155:1
      at process._tickCallback (internal/process/next_tick.js:109:7)

Environment

  • Operating System: Mac OSX
  • Ethereum client: Ganache
  • Truffle version (truffle version): 4.0.7
  • node version (node --version): 7.7.3
  • npm version (npm --version): 4.0.3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
mattlockyercommented, Apr 27, 2018

This is the only thing that worked for me:

//this is the overloaded function, the original is safeTransferFrom['address,address,uint256']
const tx = await sampleNFT.contract.safeTransferFrom['address,address,uint256,bytes'](alice, composable.address, 1, "1", { from: alice, gas: 500000 });
1reaction
aleybovichcommented, Feb 27, 2018

Ok, I’ll keep an eye. You can close it as a duplicate. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - How to override a method in unit tests that is called ...
When I am testing func1 in its unit tests, I don't want func2 to get called. So I am trying to do something...
Read more >
Override component test actions
Change the testing properties of a particular page component using -specific HTML attributes.
Read more >
Testing Your Application
Writing a Profile. All these methods have default implementations so just override the ones you need to override. Now we have defined our...
Read more >
unittest — Unit testing framework
Unittest supports skipping individual test methods and even whole classes of tests. In addition, it supports marking a test as an “expected failure,”...
Read more >
Testing
True unit tests typically run extremely quickly, as there is no runtime ... the Spring Framework provides mock objects and testing support ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found