I've started unit testing my contracts with Truffle (using Javascript). I'm fairly new to Truffle and Mocha both, and while I've tried to follow the tutorials, I'm struggling to follow exactly what's going on at each step. For instance, the following test code mostly works, but I get an error at test line 27:

whiteListLength = meta.getWhiteListLength.call();

with the error message: "cannot read property "call" of undefined". The full test is here:

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);

 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");
 //here comes the assertions

for a contract with 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;
return false;

function addToWhiteList (address voter){

Some of the code has deliberately been omitted for readability purposes, but these functions are the only relevant ones for the purposes of this test.

Sorry if this betrays an elemental lack of comprehension, but can anyone explain to me why these failures are happening? Thanks.

  • Try removing the build/ directory, sometimes truffle gets confused and it doesn't update the contracts artifacts.
    – Ismael
    Commented Mar 11, 2018 at 5:00
  • Try and double check the official web3 docs. Every resource on the internet (medium, stack exchange, google searches) is broken except for the docs Commented Mar 5, 2019 at 1:10
  • See github.com/ethereum/web3.js/issues/2443 Commented Mar 5, 2019 at 1:11

2 Answers 2


It's a little hard to tell from the formatting but it looks like the variable meta could be out scope and thus is showing as undefined. Move the declaration of meta up a level.

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

Alternatively, your instance variable meta = instance; maybe null. Can you check that with a console.log(instance);


Your .thens and asserts are outside of your it() blocks. You want to complete your sequence of contract transactions and calls and the press on with asserts, all inside it.

Try this

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

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.