[Ethereum] truffle test fails with ReferenceError: A is not defined

soliditytruffle

I am not sure what is missing. I keep getting "Reference error". Please find the details below

Step 1 :

Ubuntu# truffle compile

Step 2:

Ubuntu# truffle migrate --reset
Using network 'development'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  Migrations: 0x8bf21c1bc3fd8f192984fd03b6be50fbd145fc97
Saving successful migration to network...
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Deploying B...
  B: 0x1ca4dc712c656bc593449e1e38529e1aefbd5fec
  Deploying A...
  A: 0x54b0c1766468ae60b422fe7d7a79a2b9c84f760c
Saving successful migration to network...
Saving artifacts...

Step 3

Ubuntu# truffle test
Using network 'development'.

  Contract: A
    1) should create an object
    > No events were emitted


  0 passing (38ms)
  1 failing

  1) Contract: A should create an object:
     ReferenceError: A is not defined
      at Context.<anonymous> (a.js:6:12)
      at /usr/lib/node_modules/truffle/lib/testing/testrunner.js:125:5
      at /usr/lib/node_modules/truffle/node_modules/web3/lib/web3/property.js:119:13
      at /usr/lib/node_modules/truffle/node_modules/web3/lib/web3/requestmanager.js:89:9
      at XMLHttpRequest.request.onreadystatechange (/usr/lib/node_modules/truffle/node_modules/web3/lib/web3/httpprovider.js:119:13)
      at XMLHttpRequestEventTarget.dispatchEvent (/usr/lib/node_modules/truffle/node_modules/xhr2/lib/xhr2.js:64:18)
      at XMLHttpRequest._setReadyState (/usr/lib/node_modules/truffle/node_modules/xhr2/lib/xhr2.js:354:12)
      at XMLHttpRequest._onHttpResponseEnd (/usr/lib/node_modules/truffle/node_modules/xhr2/lib/xhr2.js:509:12)
      at IncomingMessage.<anonymous> (/usr/lib/node_modules/truffle/node_modules/xhr2/lib/xhr2.js:469:24)
      at endReadableNT (_stream_readable.js:974:12)

Reference :
Contents of a.js

contract('A', function(accounts) {

  it("should create an object", function() {

   var x = A.deployed();

    });
  });

Best Answer

You must declare the contract at the top of your file using:

var A = artifacts.require("./A.sol");

Please refer to the documentation to learn how to write Truffle tests.

Same principle can be apply in any Truffle JavaScript files (tests, migration) when using Truffle 3.x