1

I am working through DApp University's election dApp tutorial. I made unit tests for invalid candidate, and double voting. My double voting unit test is failing, saying:

  1) Contract: Election
   throws an exception for double voting:
 AssertionError: error message must contain revert
  at test\election.js:78:7
  at process._tickCallback (internal/process/next_tick.js:68:7)

Here is my dApp's repository. https://github.com/Legitimate/election

This is my code for the double voting unit test. The line that is failing is found in the first unit test I made for invalid candidate. I'm not sure why it is failing for double voting. I would appreciate any consideration from the community. Thank you.

it("throws an exception for invalid candidate", function() {
    return Election.deployed().then(function(instance) {
      electionInstance = instance;
      return electionInstance.vote(99, { from: accounts[1] });
    }).then(assert.fail).catch(function(error) {
      assert(error.message.indexOf('revert') >= 0, "error message must contain revert");
      return electionInstance.candidates(1);
    }).then(function(candidate1) {
      var voteCount = candidate1[2];
      assert.equal(voteCount, 1, "candidate 1 did not receive any votes");
      return electionInstance.candidates(2);
    }).then(function(candidate2) {
      var voteCount = candidate2[2];
      assert.equal(voteCount, 0, "candidate 2 did not receive any votes");
    });
  });



it("throws an exception for double voting", function() {
    return Election.deployed().then(function(instance) {
      electionInstance = instance;
      candidateId = 2;
      electionInstance.vote(candidateId, { from: accounts[1] });
      return electionInstance.candidates(candidateId);
    }).then(function(candidate) {
      var VoteCount = candidate[2];
      assert.equal(voteCount, 1, "accepts first vote");
      // Try to vote again
      return electionInstance.vote(candidateId, { from: accounts[1] });
    }).then(assert.fail).catch(function(error) {
      assert(error.message.indexOf('revert') >= 0, "error message must contain revert");
      return electionInstance.candidates(1);
    }).then(function(candidate1) {
      var voteCount = candidate1[2];
      assert.equal(voteCount, 1, "candidate 1 did not receive any votes");
      return electionInstance.candidates(2);
    }).then(function(candidate2) {
      var voteCount = candidate2[2];
      assert.equal(voteCount, 1, "candidate 2 did not receive any votes");
    });
  });
0
var VoteCount = candidate[2];
assert.equal(voteCount, 1, "accepts first vote");

The assert.equal() is failing because the variable VoteCount is capitalized in the declaration but not in the assert. You are comparing null to 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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