Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Nurnasib opened this issue Nov 19, 2021 · 8 comments

Comments

@Nurnasib
Copy link

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
Copy link
Author

2021-11-20 (1)
the code

@yash291
Copy link

yash291 commented Nov 28, 2021

Now it is showing assertion error: accepts first vote

@yash291
Copy link

yash291 commented Nov 28, 2021

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
Copy link

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
Copy link

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
Copy link

wallrue commented May 26, 2022

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
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants