Wie definiere ich mein Token, um den Trüffeltest zu bestehen?

Nach dem Ausführen des Trüffeltests erhalte ich die folgende Fehlermeldung:

Zanes-iMac:xyztoken zanemassey$ truffle test
Using network 'development'.

Compiling ./contracts/Migrations.sol...
Compiling ./contracts/xyzToken.sol...

Compilation warnings encountered:

/Users/zanemassey/Desktop/xyztoken/contracts/Migrations.sol:11:3: Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }" instead.
  function Migrations() public {
  ^ (Relevant source part starts here and spans across multiple lines).
,/Users/zanemassey/Desktop/xyztoken/contracts/xyzToken.sol:8:2: Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }" instead.
    function xyzToken (uint256 _initialSupply) public {
 ^ (Relevant source part starts here and spans across multiple lines).



  Contract: xyzToken
    1) sets the total supply upon deployment
    > No events were emitted


  0 passing (36ms)
  1 failing

  1) Contract: xyzToken
       sets the total supply upon deployment:
     ReferenceError: xyzToken is not defined
      at Context.<anonymous> (test/xyzToken.js:7:3)
      at /usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-core/lib/testing/testrunner.js:135:1
      at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/property.js:119:1
      at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/requestmanager.js:89:1
      at /usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-provider/wrapper.js:134:1
      at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/httpprovider.js:128:1)
      at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:64:1)
      at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:354:1)
      at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:509:1)
      at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:469:1)
      at endReadableNT (_stream_readable.js:1081:12)
      at process._tickCallback (internal/process/next_tick.js:63:19)

Wenn keine Ereignisse ausgegeben wurden, was genau schlägt dann fehl?

das ist mein js:

var DappToken = artifacts.require("./xyzToken.sol");

contract('xyzToken', function(accounts) {
    var tokenInstance;

    it('sets the total supply upon deployment', function() {
        return xyzToken.deployed().then(function(instance) {
          tokenInstance = instance;
          return tokenInstance.totalSupply();
        }).then(function(totalSupply) {
          assert.equal(totalSupply.toNumber(), 600000000, 'sets the total supply to 600,000,000');
          return tokenInstance.balanceOf(accounts[0]);
        }).then(function(adminBalance) {
          assert.equal(adminBalance.toNumber(), 600000000, 'it allocates the inital supply to the admin account');
        });
    });
});

und Festigkeit:

pragma solidity ^0.4.23;

contract xyzToken {
    uint256 public totalSupply;

    mapping(address => uint256) public balanceOf;

    function xyzToken (uint256 _initialSupply) public {
        balanceOf[msg.sender] = _initialSupply;
        totalSupply = _initialSupply;
    }
}

Ich habe die Gesamtversorgung für Anzahl und Admin-Guthaben geltend gemacht und Ganache-Konten mit einem assoziativen Array konfiguriert. Was genau ist an meinem Token nicht definiert?

jede Hilfe wäre willkommen

Antworten (1)

Die oberste Zeile Ihres Javascripts ist falsch. Sie haben das Artefakt aufgerufen, DappTokenaber versucht, es mit zu instanziieren xyzToken, was nicht definiert ist, und daher der Fehler, den Sie erhalten.

Ziehen Sie auch in Betracht, wie folgt zu async/await zu wechseln – es macht die Tests für die Augen viel einfacher:

const DappToken = artifacts.require("./xyzToken.sol");

contract('xyzToken', accounts => {

  it('sets the total supply upon deployment', async () => {
    const tokenInstance = await DappToken.deployed()
        , initSupply    = 600000000
        , totalSupply   = await tokenInstance.totalSupply()
        , adminBalance  = await tokenInstance.balanceOf(accounts[0])
    assert.equal(totalSupply.toNumber(), initSupply, `Total supply should be ${initSupply}!`)
    assert.equal(adminBalance.toNumber(), initSupply, 'Initial supply should be allocated to admin account!')
  })
})