Solidity

測試在松露中不起作用

  • March 14, 2021

執行松露測試時出錯

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)

我的程式碼是:

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

測試文件:

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

版本:

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

不需要 getAge4() 方法,因為屬性 age4 是公共的。

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

}

順便說一下,常量名應該是大寫的 SNAKE_CASE。

改進的測試案例

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

}

預期結果

$ 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)

引用自:https://ethereum.stackexchange.com/questions/94624