Probleme beim Testen von Trüffeln mit Javascript

Ich habe begonnen, meine Verträge mit Truffle zu testen (unter Verwendung von Javascript). Ich bin ziemlich neu bei Truffle und Mocha, und obwohl ich versucht habe, den Tutorials zu folgen, habe ich Mühe, genau zu folgen, was bei jedem Schritt vor sich geht. Zum Beispiel funktioniert der folgende Testcode meistens, aber ich erhalte eine Fehlermeldung in Testzeile 27:

whiteListLength = meta.getWhiteListLength.call();

mit der Fehlermeldung: "cannot read property "call" of undefined". Den vollständigen Test gibt es hier:

it("should add the correct account to the whitelist", function(){
  var account_one = accounts[0];
  var whiteListLength;
  var isAccountWhiteListed;
  var meta;

  return EMBallot.deployed().then(function(instance) { 
      meta = instance;
  return meta.addToWhiteList.call(account_one);
 })

.then(function(){
 whiteListLength = meta.getWhiteListLength.call();//this line is the problem
 return meta.amIWhitelisted.call(account_one); 
})

.then(function(response) {
  isAccountWhiteListed = response;

  assert.equal(whiteListLength, 1, "Whitelist should have exactly one member");
  assert.isTrue(isAccountWhiteListed);
 //here comes the assertions
});

für einen Vertrag mit Code:

pragma solidity ^0.4.19;

contract EMBallot {

address[] whiteList;
 struct Proposal {
    uint voteCount;//how many votes this proposal has received
    string description;//what this option is about, what you are voting for
}
 struct Voter {
    bool voted;//if true, has already voted and any further attempts to vote are automatically ignored
    uint8 vote;
    string name;
}
Proposal[] proposals;  

address admin; //there should only be one admin per election, this variable stores the address designated as the admin.    
mapping(address => Voter) voters;//this creates a key-value pair, where addresses are the keys, 
and Voter structs(objects) are the values. 

function EMBallot() public{
admin = msg.sender;
}

function getWhiteListLength() constant returns(uint256){
    return whiteList.length;
}

function amIWhitelisted(address myAddress) constant returns(bool){
for(uint i=0; i<=whiteList.length; i++){//iterate over whiteList
if(myAddress == whiteList[i]){//i checked, you CAN use == on addresses
return true;
break;
}
return false;
}}

function addToWhiteList (address voter){
whiteList.push(voter);   
}
}

Ein Teil des Codes wurde aus Gründen der Lesbarkeit bewusst weggelassen, aber diese Funktionen sind die einzigen, die für die Zwecke dieses Tests relevant sind.

Entschuldigung, wenn dies einen elementaren Mangel an Verständnis verrät, aber kann mir jemand erklären, warum diese Fehler auftreten? Danke.

Versuchen Sie, das build/-Verzeichnis zu entfernen, manchmal wird Truffle verwirrt und die Vertragsartefakte werden nicht aktualisiert.
Versuchen Sie, die offiziellen web3-Dokumente zu überprüfen. Jede Ressource im Internet (Medium, Stapelaustausch, Google-Suche) ist außer den Dokumenten kaputt

Antworten (2)

Es ist ein wenig schwer anhand der Formatierung zu erkennen, aber es sieht so aus, als ob die Variable metaaußerhalb des Gültigkeitsbereichs liegen könnte und daher als undefiniert angezeigt wird. Verschieben Sie die Deklaration um metaeine Ebene nach oben.

var meta;    
it("should add the correct account to the whitelist", function(){
...

Alternativ kann Ihre Instanzvariable meta = instance;null sein. Kannst du das überprüfen mit aconsole.log(instance);

Ihre .thensund Asserts befinden sich außerhalb Ihrer it()Blöcke. Sie möchten Ihre Abfolge von Vertragstransaktionen und Anrufen abschließen und mit Asserts weitermachen, alles drin it.

Versuche dies

var myContact;
var response1;
var response2;

it("should ...", function() {
  MyContract.deployed() // no return
  .then(function(instance) {
    myContract = instance;
    return myContract.doSomething()
  })
  .then(function(response) {
    response1 = response;
    return myContract.somethingElse()
  })
  .then(function(response) {
    response2 = response;
    assert.equal(response1.toString(10),2,"response1 should be 2");
    assert.strictEqual(response2,false,"response2 isn't false");
  });
}); // this closes the it() block

Ich hoffe es hilft.