Search code examples
soliditysmartcontractstruffle

Can't test simple Truffle project


I'm trying to create a simple solidity project using truffle but I'm having problems :(

Projects $ nvm current
v16.16.0
Projects $ mkdir SimpleProject
Projects $ cd SimpleProject
SimpleProject $ truffle init

Starting init...
================

> Copying project files to /home/personal/Projects/SimpleProject

Init successful, sweet!

Try our scaffold commands to get started:
  $ truffle create contract YourContractName # scaffold a contract
  $ truffle create test YourTestName         # scaffold a test

http://trufflesuite.com/docs

SimpleProject $
SimpleProject $ truffle create contract SimpleProject
SimpleProject $ truffle create test TestSimpleProject
SimpleProject $
SimpleProject $ truffle test

Compiling your contracts...
===========================
> Compiling ./contracts/Migrations.sol
> Compiling ./contracts/SimpleProject.sol
> Compilation warnings encountered:

    Warning: Visibility for constructor is ignored. If you want the contract to be non-deployable, making it "abstract" is sufficient.
 --> project:/contracts/SimpleProject.sol:5:3:
  |
5 |   constructor() public {
  |   ^ (Relevant source part starts here and spans across multiple lines).


> Artifacts written to /tmp/test--16503-cBYOoJTkVAN1
> Compiled successfully using:
   - solc: 0.8.15+commit.e14f2714.Emscripten.clang
Error: Could not find artifacts for TestSimpleProject from any sources
    at Resolver.require (/home/personal/.nvm/versions/node/v16.16.0/lib/node_modules/truffle/build/webpack:/packages/resolver/dist/lib/resolver.js:60:1)
    at Object.require (/home/personal/.nvm/versions/node/v16.16.0/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/Test.js:280:1)
    at Object.<anonymous> (/home/personal/Projects/SimpleProject/test/test_simple_project.js:1:37)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at /home/personal/.nvm/versions/node/v16.16.0/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:417:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/home/personal/.nvm/versions/node/v16.16.0/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:414:14)
    at Mocha.run (/home/personal/.nvm/versions/node/v16.16.0/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:1015:10)
    at /home/personal/.nvm/versions/node/v16.16.0/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/Test.js:156:1
    at new Promise (<anonymous>)
    at Object.run (/home/personal/.nvm/versions/node/v16.16.0/lib/node_modules/truffle/build/webpack:/packages/core/lib/testing/Test.js:155:1)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at startGanacheAndRunTests (/home/personal/.nvm/versions/node/v16.16.0/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/test/run.js:49:1)
    at Object.module.exports [as run] (/home/personal/.nvm/versions/node/v16.16.0/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/test/run.js:106:1)
    at runCommand (/home/personal/.nvm/versions/node/v16.16.0/lib/node_modules/truffle/build/webpack:/packages/core/lib/command-utils.js:184:1)
Truffle v5.5.22 (core: 5.5.22)
Node v16.16.0
SimpleProject $

Solution

  • truffle create test TestSimpleProject command creates a file test/test_simple_project.js with the following contents:

    const TestSimpleProject = artifacts.require("TestSimpleProject");
    
    /*
     * uncomment accounts to access the test accounts made available by the
     * Ethereum client
     * See docs: https://www.trufflesuite.com/docs/truffle/testing/writing-tests-in-javascript
     */
    contract("TestSimpleProject", function (/* accounts */) {
      it("should assert true", async function () {
        await TestSimpleProject.deployed();
        return assert.isTrue(true);
      });
    });
    

    On the first line, you can see that it's trying to load a contract named TestSimpleProject - the same contract name as you specified the test name. But there's no contract with this name in your project.


    A simple fix is to change the name of the contract required for the test.

    test/test_simple_project.js

    const TestSimpleProject = artifacts.require("SimpleProject");
    

    Or you can pass an existing contract name when you're creating the test.

    truffle create test SimpleProject
    

    Either way, don't forget to create a migration file for this contract.

    migrations/2_simple_project.js

    const SimpleProject = artifacts.require("SimpleProject");
    
    module.exports = function (deployer) {
      deployer.deploy(SimpleProject);
    };