0

I was using "truffle test" for testing with JS. gives this error. here is the code of that part.



it('transfer success', function(){
    return Eve.deployed().then(function(instance){
        tokenInstance = instance;
        // testing "require" first by sending larger than the senders balance
        return tokenInstance.transfer.call(accounts[1], 9999999999999);
    }).then(assert.fail).catch(function(error){
         assert(error.message.indexOf('revert' >= 0, 'error message must contain revert'));
         return tokenInstance.transfer.call(accounts[1], 250000, { from: accounts[0] });
    }).then(function(success){
        assert.equal(success, true,'returns true');
        return tokenInstance.transfer(accounts[1], 250000, { from: accounts[0] });
    }).then(function(receipt){
        assert.equal(receipt.logs[0].length,1, 'transfer one event');
        assert.equal(receipt.logs[0].event, 'Transfer', 'the "Transfer" event');
        assert.equal(receipt.logs[0].args._from, accounts[0], 'logs the account thr tokens are transferred from');
        assert.equal(receipt.logs[0].args._to, accounts[1], 'logs the account thr token are transferred to');
        assert.equal(receipt.logs[0].args._value,250000, 'logs the transfer amount');
        return tokenInstance.balanceOf(accounts[1]);
    }).then(function(balance){
        assert.equal(balance.toNumber(), 250000,'adds amount to recevers account');
        return tokenInstance.balanceOf(accounts[0]);
    }).then(function(balance){
        assert.equal(balance.toNumber(), (10000000 - 250000),'remaining balance of senders account');
    });
});

upon "truffle test" gives the following error.

ReferenceError: accounts is not defined

background : creating erc20 token as per standards. the first 2 tests run successfully How can i troubleshoot this?

3
  • Can you show the full js file ?
    – clement
    Sep 20, 2020 at 13:46
  • There is a problem with the definition of 'accounts' which should be defined at the top of your test in the 'contract' function.
    – clement
    Sep 20, 2020 at 13:49
  • i had done mistake in parenthesis, initialization was correct with contract("MyContract", function(accounts) { ...});
    – vivek
    Sep 20, 2020 at 15:42

2 Answers 2

1

Encapsulate it all with:

contract("MyContract", function(accounts) {
   ...
});
1
  • yep had done that right
    – vivek
    Sep 20, 2020 at 15:40
0

How it actually worked for me :

contract("owner()", (accounts) => {
it("returns the address of the owner", async () => {
    const greeter = await GreeterContract.deployed();
    const owner = await greeter.owner();
    assert(owner, "the current owner");
});

it("matches the address that originally deployed the contract", async () => {
    const greeter = await GreeterContract.deployed();
    const owner = await greeter.owner();
    const expected = accounts[0];
    assert.equal(owner, expected, "matches address used to deploy contract");
});

});

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 8, 2022 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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