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

Truffle Test ReferenceError: accounts not defined #41

Open
degbuniwe opened this issue Jul 30, 2020 · 1 comment
Open

Truffle Test ReferenceError: accounts not defined #41

degbuniwe opened this issue Jul 30, 2020 · 1 comment

Comments

@degbuniwe
Copy link

degbuniwe commented Jul 30, 2020

I am following an Election.sol tutorial and when I tried to test the function below, I get ReferenceError : accounts not defined. I'm not sure how to fix this.

var Voting = artifacts.require("./Voting.sol");

contract("Voting", function(accounts){
var votingInstance;

//to check initialisation

it("initialises with two candidates", function() {
	return Voting.deployed().then(function(instance){
		return instance.candidatesCount();
	}).then(function(count){
		assert.equal(count, 2);
	});
});

});

it("it initialises the candidates with the correct values", function() {
	return Voting.deployed().then(function(instance) {
		votingInstance = instance;
		return votingInstance.candidates(1);

	}) .then(function(candidate) {
		assert.equal(candidate[0], 1, "contains the correct id");
		assert.equal(candidate[1], "Richard Denton", "contains the correct name");
		assert.equal(candidate[2], 0, "contains the correct vote count");
		return votingInstance.candidates(2);
	}).then(function(candidate){
		assert.equal(candidate[0], 2, "contains the correct id");
		assert.equal(candidate[1], "Jeff Bridges", "contains the correct name");
		assert.equal(candidate[2], 0, "contains the correct vote count");
	});

});
           it("allows a voter to cast a vote", function() {
          return Voting.deployed().then(function(instance) {
          votingInstance = instance;
          candidateId = 1;
        return votingInstance.vote.call(candidateId, { from: accounts[0] });
}).then(function(receipt) {
  assert.equal(receipt.logs.length, 1, "an event was triggered");
  assert.equal(receipt.logs[0].event, "votedEvent", "the event type is correct");
  assert.equal(receipt.logs[0].args._candidateId.toNumber(), candidateId, "the candidate id is correct");
  return votingInstance.voters(accounts[0]);
}).then(function(voted) {
  assert(voted, "the voter was marked as voted");
  return votingInstance.candidates(candidateId);
}).then(function(candidate) {
  var voteCount = candidate[2];
  assert.equal(voteCount, 1, "increments the candidate's vote count");
})

});

@0xBlueshiftLabs
Copy link

Try the following, if you haven't already sorted it:

contract("Voting", (accounts) => {

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

2 participants