Comencé a probar mis contratos con Truffle (usando Javascript). Soy bastante nuevo en Truffle y Mocha, y aunque he tratado de seguir los tutoriales, me cuesta seguir exactamente lo que sucede en cada paso. Por ejemplo, el siguiente código de prueba funciona principalmente, pero aparece un error en la línea de prueba 27:
whiteListLength = meta.getWhiteListLength.call();
con el mensaje de error: "no se puede leer la propiedad" llamada "de indefinido". La prueba completa está aquí:
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
});
para un contrato con código:
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);
}
}
Parte del código se ha omitido deliberadamente por motivos de legibilidad, pero estas funciones son las únicas relevantes para los fines de esta prueba.
Lo siento si esto delata una falta elemental de comprensión, pero ¿alguien puede explicarme por qué están ocurriendo estas fallas? Gracias.
Es un poco difícil saberlo por el formato, pero parece que la variable meta
podría estar fuera del alcance y, por lo tanto, se muestra como indefinida. Mover la declaración de meta
un nivel superior.
var meta;
it("should add the correct account to the whitelist", function(){
...
Alternativamente, su variable de instancia meta = instance;
puede ser nula. ¿Puedes comprobar eso con unconsole.log(instance);
Tus .thens
afirmaciones y están fuera de tus it()
bloques. Desea completar su secuencia de transacciones de contrato y llamadas y presionar con afirmaciones, todo dentro it
.
Prueba esto
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
Espero eso ayude.
ismael
nick carraway
nick carraway