dappuniversity / election

A Decentralized Ethereum Voting Application Tutorial
563 stars 506 forks source link

throws an exception for double voting. AssertionError: accepts first vote: expected <BN: 0> to equal 1 #57

Open Nurnasib opened 2 years ago

Nurnasib commented 2 years ago

2021-11-20

Truffle test failing because in 'throws an exception for double voting'. AssertionError: accepts first vote: expected <BN: 0> to equal 1

Nurnasib commented 2 years ago

2021-11-20 (1) the code

yash291 commented 2 years ago

Now it is showing assertion error: accepts first vote

yash291 commented 2 years ago

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, "0x00" , "accepts first vote"); // Try to vote again return electionInstance.vote(candidateId, { from: accounts[1] }); }).then(function(receipt) { assert.equal(receipt.receipt.status, "0x00", "failed Transaction"); 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"); }); }); });

This is the code...

hiraahmad6 commented 2 years ago

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, "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"); }); }); });

My last test executes successfully by changing the code as written above.

SHIVANGISINGH1 commented 2 years ago
it("throws an exception for double voting", function() {
        return Election.deployed().then(function(instance) {
            CandidatesInstance = instance;
            CandidateId = 2;
            CandidatesInstance.vote(CandidateId, {from: accounts[3]});
            return CandidatesInstance.candidates(CandidateId);
        }).then(function(recipt) {
            return CandidatesInstance.voters(accounts[3]);
        }).then(function(voteAdded) {
            assert("The voter casted his vote");
            return CandidatesInstance.candidates(CandidateId);
        }).then(function(candidate2) {
            let voteCount = candidate2[2];
            assert.equal(voteCount, 1, "accepts the 1st vote");
            // If tries to vote again
            CandidateId = 2;
            return CandidatesInstance.vote(CandidateId, {from: accounts[3]});
        }).then(assert.fail).catch(function(error) {
            assert(error.message, "Error message must contain revert");
            return CandidatesInstance.candidates(1);
        }).then(function(candidate1) {
            let voteCount = candidate1[2];
            assert.equal(voteCount, 1, "Candidate 1 did not recieve any vote");
            return CandidatesInstance.candidates(2);
        }).then(function(candidate2) {
            let voteCount = candidate2[2];
            assert.equal(voteCount, 1, "Candidate 2 did not recieve any vote");
        });
    })
`This code passes the test cases, although I think making this an asynchronous function will help with this issue.`
wallrue commented 2 years ago

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, "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"); }); }); });

My last test executes successfully by changing the code as written above.

This approach worked. Simply, we change "assert.equal(voteCount, 1, "candidate 2 did not receive any votes");" to "assert.equal(voteCount, 0, "candidate 2 did not receive any votes");"

reduanmasud commented 1 year ago
it("throws an exception for double voting", ()=> {
        return Election.deployed().then(instance => {
            electionInstance = instance;
            candidateId = 2;
            return electionInstance.vote(candidateId, {from: accounts[2]});
        }).then(receipt => {
            //console.log(receipt)
            return electionInstance.voters(accounts[2]);
        }).then(voted => {
            assert(voted, "Vote Successfully given");
            return electionInstance.vote(candidateId, {from:accounts[2]});
        }).then(assert.fail).catch(error=>{
            //console.log(error);
            assert(error.message.indexOf("revert") >= 0, "Double Vote Not Allowed");
            return electionInstance.candidates(1);
        }).then(candidate1 => {
            var voteCount = candidate1[2];
            assert.equal(voteCount, 1, "Candidate 1 did not receive any votes");
            return electionInstance.candidates(2);
        }).then(candidate2 => {
            var voteCount = candidate2[2];
            assert.equal(voteCount, 1, "Candidate 2 did not reveive any vote");
        })
      });

Use this code. This works fine for me.

Demiladekusa commented 1 year ago
it("throws an exception for double voting", ()=> {
        return Election.deployed().then(instance => {
            electionInstance = instance;
            candidateId = 2;
            return electionInstance.vote(candidateId, {from: accounts[2]});
        }).then(receipt => {
            //console.log(receipt)
            return electionInstance.voters(accounts[2]);
        }).then(voted => {
            assert(voted, "Vote Successfully given");
            return electionInstance.vote(candidateId, {from:accounts[2]});
        }).then(assert.fail).catch(error=>{
            //console.log(error);
            assert(error.message.indexOf("revert") >= 0, "Double Vote Not Allowed");
            return electionInstance.candidates(1);
        }).then(candidate1 => {
            var voteCount = candidate1[2];
            assert.equal(voteCount, 1, "Candidate 1 did not receive any votes");
            return electionInstance.candidates(2);
        }).then(candidate2 => {
            var voteCount = candidate2[2];
            assert.equal(voteCount, 1, "Candidate 2 did not reveive any vote");
        })
      });

Use this code. This works fine for me.

hey please can you share your repo