1

Got his error when run truffle test

Using network 'test'.

Compiling your contracts...
===========================
> Compiling .\test\TestFirst.sol
> Compilation warnings encountered:

    /D/Truffle Projects/statevariables/contracts/First.sol:21:6: Warning: Function state mutability can be restricted to pure
     function getAge4() public view returns (int) {
     ^ (Relevant source part starts here and spans across multiple lines).

> Artifacts written to C:\Users\92311\AppData\Local\Temp\test--9552-BsbV54PdVc1Y
> Compiled successfully using:
   - solc: 0.5.16+commit.9c3226ce.Emscripten.clang

  TestFirst
    1) testAge
    > No events were emitted
 
  0 passing (20s)
  1 failing

  1) TestFirst
       testAge:
     Error: Returned error: VM Exception while processing transaction: revert
      at Context.<anonymous> (C:\Users\92311\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\testing\SolidityTest.js:92:1)
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (internal/process/task_queues.js:93:5)

My code is:

pragma solidity ^0.5.0;

contract First {
    int internal age = 40;
    int public age1 = 50;
    int private age2 = 60;
    int public constant age3 = 70;

    function getAge1() public view returns (int) {
        return age;
    }

     function getAge2() public view returns (int) {
        return age1;
    }

     function getAge3() public view returns (int) {
        return age2;
    }

     function getAge4() public view returns (int) {
        return age3;
    }
}

Test file:

pragma solidity ^0.5.0;

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

contract TestFirst {
    function testAge() public {
        First meta = First(DeployedAddresses.First());
        meta.getAge1();
        Assert.equal(meta.getAge1(), 40, "Age 1 is not equal to 40");
    }
    
}

Version:

Truffle v5.2.4 (core: 5.2.4)
Solidity v0.5.16 (solc-js)
Node v14.16.0
Web3.js v1.2.9
0

The method getAge4() is not needed, because the attribute age4 is public.

pragma solidity ^0.5.0;

contract First {
    int internal age1 = 40;
    int public age2 = 50;
    int private age3 = 60;
    int public constant age4 = 70;

    function getAge1() public view returns (int) {
        return age1;
    }

    function getAge2() public view returns (int) {
        return age2;
    }

    function getAge3() public view returns (int) {
        return age3;
    }

}

By the way, the constant name should be in capitalised SNAKE_CASE.

Improved test cases

pragma solidity ^0.5.0;

import "truffle/Assert.sol";
import "../contracts/First.sol";

contract TestFirst {

    First private first;

    address private owner;

    constructor() public {
        first = new First();
        owner = address(this);
    }

    function testAge1() public {
        Assert.equal(first.getAge1(), 40, "Age 1 is not equal to 40");
    }

    function testAge2() public {
        Assert.equal(first.getAge2(), 50, "Age 2 is not equal to 50");
    }

    function testAge3() public {
        Assert.equal(first.getAge3(), 60, "Age 3 is not equal to 60");
    }

    function testAge4() public {
        Assert.equal(first.age4(), 70, "Age 4 is not equal to 70");
    }

}

Expected result

$ truffle compile

Compiling your contracts...
===========================
> Compiling ./contracts/First.sol
> Artifacts written to /Users/markus/Projects/IntelliJ/cbc/contracts/build/contracts
> Compiled successfully using:
   - solc: 0.5.17+commit.d19bba13.Emscripten.clang



$ truffle test   
Using network 'development'.


Compiling your contracts...
===========================
> Compiling ./test/FirstTest.sol
> Artifacts written to /var/folders/21/bjywjj2j1k34ctd61czlz_gc0000gn/T/test--6570-S6arXx4xH1b8
> Compiled successfully using:
   - solc: 0.5.17+commit.d19bba13.Emscripten.clang

development


  TestFirst
    ✓ testAge1 (125ms)
    ✓ testAge2 (56ms)
    ✓ testAge3 (60ms)
    ✓ testAge4 (58ms)


  4 passing (6s)


6
  • "Source file requires different compiler version (current compiler is 0.8.2+commit.661d1103.Emscripten.clang) - note that nightly builds are considered to be strictly less than the released version" I'm getting this error. – Dummy Turtle Mar 14 at 12:45
  • My contract is getting compiled but when i run truffle test it shows; Compiling your contracts... > Compiling .\test\TestFirst.sol > Artifacts written to C:\Users\92311\AppData\Local\Temp\test--12308-8u2MBlQzBDJj > Compiled successfully using: - solc: 0.5.16+commit.9c3226ce.Emscripten.clang TestFirst 1) testAge > No events were emitted 0 passing (19s) 1 failing 1) TestFirst testAge: Error: Returned error: VM Exception while processing transaction: revert – Dummy Turtle Mar 14 at 12:46
  • Be aware that the import "truffle/Assert.sol" is different in my version and there is a different solitary version used in the test case. – Markus Sprunck Mar 14 at 12:53
  • Sir it's my second day that I have started learning solidity and I'm using vs code. Can you tell which compiler version should I use – Dummy Turtle Mar 14 at 13:10
  • Currently Compiler installed is V0.5.16 (solc-js) so which version should i mention in my projects and tests ? – Dummy Turtle Mar 14 at 13:16

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.