Closed13

「SolidityとEthereumによる実践スマートコントラクト開発」の沼3

ikmzkroikmzkro

4.1 セットアップ

階層構造

sekai@XXXXX:~/greeter$ tree
.
├── contracts
│   └── Migrations.sol
├── migrations
│   └── 1_initial_migration.js
├── test
│   └── greeter_test.js
└── truffle-config.js

3 directories, 4 files
ikmzkroikmzkro

4.2 最初のテスト

以下のコードをgreeter/test/greeter_test.jsに追記した。

const GreeterContract = artifacts.require("Greeter");

contract("Greeter", () => {
    it("has been deployed successfully", async () => {
      const greeter = await GreeterContract.deployed();
      assert(greeter, "contract failed to deploy");
    });
  });
ikmzkroikmzkro

実行したが応答がない

sekai@XXXXX:~/greeter$ truffle test


shinriyoshinriyo

P54のですよね。
本書ではGreeterがない旨のエラーが出るらしいですが動かないですよね・・。

ikmzkroikmzkro

ありがとうございます!
自分でも再度検証してみます。

ikmzkroikmzkro

応答がでるようになりましたね。

/greeter$ truffle test

Compiling your contracts...
===========================
> Compiling ./contracts/Migrations.sol
> Artifacts written to /tmp/test--1938-wSClWNsNdPfO
> Compiled successfully using:
   - solc: 0.8.9+commit.e5eed63a.Emscripten.clang

Error: Could not find artifacts for Greeter from any sources
    at Resolver.require (/home/sekai/.nvm/versions/node/v14.18.0/lib/node_modules/truffle/build/webpack:/packages/resolver/dist/lib/resolver.js:61:1)
    at Object.require (/home/sekai/.nvm/versions/node/v14.18.0/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/Test.js:281:1)
    at Object.<anonymous> (/home/sekai/greeter/test/greeter_test.js:1:35)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at /home/sekai/.nvm/versions/node/v14.18.0/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:390:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/home/sekai/.nvm/versions/node/v14.18.0/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:387:14)
    at Mocha.run (/home/sekai/.nvm/versions/node/v14.18.0/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:961:10)
    at /home/sekai/.nvm/versions/node/v14.18.0/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/Test.js:157:1
    at new Promise (<anonymous>)
    at Object.run (/home/sekai/.nvm/versions/node/v14.18.0/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/Test.js:156:1)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at Object.run (/home/sekai/.nvm/versions/node/v14.18.0/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/test/index.js:208:1)
    at Command.run (/home/sekai/.nvm/versions/node/v14.18.0/lib/node_modules/truffle/build/webpack:/packages/core/lib/command.js:183:1)
Truffle v5.4.16 (core: 5.4.16)
Node v14.18.0
ikmzkroikmzkro

前回(2021/06/19)と異なる点としては、Truffle, Nodeのバージョンだと思われます。
本書と私の環境では下記のバージョン差がありますが、上記のようにテストが動くようになりました。

本書の環境

  • Truffle v5.1.31 (core: 5.1.30)
  • Node v10.21.0

私の環境

  • Truffle v5.4.16 (core: 5.4.16)
  • Node v14.18.0
ikmzkroikmzkro

引き続きテストしていく

test/greeter_test.js
pragma solidity >= 0.4.0 < 0.7.0;

contract Greeter{
  
}
ikmzkroikmzkro

本書とは異なる応答が出るね

Compiling your contracts...
===========================
> Compiling ./contracts/Greeter.sol
> Compiling ./contracts/Migrations.sol

ParserError: Source file requires different compiler version (current compiler is 0.8.9+commit.e5eed63a.Emscripten.clang) - note that nightly builds are considered to be strictly less than the released version
 --> project:/contracts/Greeter.sol:1:1:
  |
1 | pragma solidity >= 0.4.0 < 0.7.0;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


Error: Truffle is currently using solc 0.8.9, but one or more of your contracts specify "pragma solidity >= 0.4.0 < 0.7.0".
Please update your truffle config or pragma statement(s).
(See https://trufflesuite.com/docs/truffle/reference/configuration#compiler-configuration for information on
configuring Truffle to use a specific solc compiler version.)

Compilation failed. See above.
Truffle v5.4.16 (core: 5.4.16)
Node v14.18.0
このスクラップは2022/05/28にクローズされました