Truffle test fails with Error: Could not find artifacts

Viewed 3559

I learn the demo: Code is come from: https://truffleframework.com/tutorials/pet-shop, when I test:

$ truffle.cmd test
Using network 'development'.

Compiling .\test\TestAdoption.sol...


TestAdoption
1) "before all" hook: prepare suite


0 passing (30s)
1 failing

1) TestAdoption
   "before all" hook: prepare suite:
Error: Could not find artifacts for /E/blockchain/pet-            
shop/contracts/Adoption.sol from any sources
at Resolver.require (D:\nvm\v10.14.2\node_modules\truffle\build\webpack:\packages\truffle-resolver\index.js:37:1)
  at TestResolver.require (D:\nvm\v10.14.2\node_modules\truffle\build\webpack:\packages\truffle-core\lib\testing\testresolver.js:17:1)
  at TestResolver.require (D:\nvm\v10.14.2\node_modules\truffle\build\webpack:\packages\truffle-core\lib\testing\testresolver.js:17:1)
  at dependency_paths.forEach.dependency_path (D:\nvm\v10.14.2\node_modules\truffle\build\webpack:\packages\truffle-core\lib\testing\soliditytest.js:203:1)
  at Array.forEach (<anonymous>)
  at deployer.deploy.then (D:\nvm\v10.14.2\node_modules\truffle\build\webpack:\packages\truffle-core\lib\testing\soliditytest.js:202:1)
  at D:\nvm\v10.14.2\node_modules\truffle\build\webpack:\packages\truffle-deployer\src\deferredchain.js:20:1
  at process._tickCallback (internal/process/next_tick.js:68:7)

I updated my nodejs lastest, and installed window-build-tools,it does not work.

TestAdoption.sol:

pragma solidity ^0.5.0;

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

contract TestAdoption {
    Adoption adoption = Adoption(DeployedAddresses.Adoption());
    function testUserCanAdoptPet() public {
        uint returnedId = adoption.adopt(expectedPetId);

        Assert.equal(returnedId, expectedPetId);
    }
    uint expectedPetId = 8;

    address expectedAdopter = address(this);
    function testGetAdopterAddressByPetId() public {
        address adopter = adoption.adopters(expectedPetId);
        Assert.equal(adopter, expectedAdopter, "Owner of the expected pet should be this contract");
    }

    function testGetAdopterAddressByPetIdInArray() public {
        address[16] memory adopters = adoption.getAdopters();
        Assert.equal(adopters[expectedPetId], expectedAdopter, "Owner of the expected pet should be this contract");
    }    

}

2_deploy_contracts.sol:

var Adoption = artifacts.require("Adoption");

module.exports = function(deployer) {
    deployer.deploy(Adoption);
};

And import "truffle/Assert.sol"; vscode say: Source "truffle/Assert.sol" not found: File import callback not supported.My friend's version is 0.4.14 and work well, may be a version problem?

enter image description here

Here is project dir(just a demo from https://truffleframework.com/tutorials/pet-shop):

enter image description here

3 Answers

This error DOESN'T happen (currently) in the version v5.1.10 of truffle.

My full error was:

TypeError: Error parsing C:/Users/PATH/yourSmartContract.sol: Cannot destructure property 'body' of 'undefined' as it is undefined.

My solution is to downgrade the version like so:

  1. $ npm uninstall -g truffle
  2. $ npm install -g truffle@v5.1.10

(Developing in BC is hard because of version control and managment. Keep the good the work.)

The problem is the name of the artifact is defined according to the contract's name. This is your contract:

contract TestAdoption {

}

So;

2_deploy_contracts.sol:

// not artifactsrequire(Adoption)
var Adoption = artifacts.require("TestAdoption");

module.exports = function(deployer) {
    deployer.deploy(Adoption);
};

Try these files:

1_initial_migration.js:

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

module.exports = function(deployer) {
  deployer.deploy(Migrations);
};

2_deploy_contracts.js:

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

module.exports = function(deployer) {
  deployer.deploy(YourContractName);
};

Tips:

  • Do not change the name of the files (1_initial_migration.js and 2_deploy_contracts.js);
  • Use the exactly name of your contract in the "2_deploy_contracts" file (if it is called BatatinhaFrita123, you need to specify BatatinhaFrita123 inside the artifacts.require()).
Related