Open Nurnasib opened 2 years ago
the code
Now it is showing assertion error: accepts first vote
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...
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.
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.`
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");"
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.
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
Truffle test failing because in 'throws an exception for double voting'. AssertionError: accepts first vote: expected <BN: 0> to equal 1