0

Just started with unit testing after creating my token, but the test cases are passing even if I add a number in the equals bracket.

In test one, if I change to expect(await instance.balanceOf(deployerAccount)).to.be.a.bignumber.equal(totalSupply+1); then the test case fails, but not when using eventually.

Doing the same as above for other test case, does not work, they simply fail even when correct.

What am I missing out?

contract("Token Test",async (accounts)=>{

    const[deployerAccount, recepient, anotherAccount]=accounts;

    it("one-all tokens should be in my account", async () => {
        let instance = await Token.deployed();
        let totalSupply =await instance.totalSupply();
        expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(totalSupply);
    })

    it("two-is possible to send token between accounts", async () => {

        const sendTokens=1;
        let instance= await Token.deployed();
        let totalSupply = await instance.totalSupply()+1;
        expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(totalSupply);
        expect(instance.transfer(recepient, sendTokens)).to.eventually.be.fulfilled;
        expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(totalSupply.sub(new BN(sendTokens)));
        expect(instance.balanceOf(recepient)).to.eventually.be.a.bignumber.equal(new BN(sendTokens));
      
    })

    it("three-is not possible to send more tokens than available in total", async()=>{
        let instance=await Token.deployed();
        let balanceOfDeployer=await instance.balanceOf(deployerAccount);
        expect(instance.transfer(recepient,new BN(balanceOfDeployer+1))).to.be.rejected;
        expect(instance.balanceOf(deployerAccount)).to.be.bignumber.equal(balanceOfDeployer);
    })


});

Did I make sense? please help!

1

1 Answer 1

0

Solved it by adding a before each() function and replacing the instance.

contract("Token Test",async (accounts)=>{
    

    const[deployerAccount, recipient, anotherAccount]=accounts;

    beforeEach(async()=>{
        this.myToken=await Token.new(process.env.INITIAL_TOKENS);
    })

    it("all tokens should be in my account", async () => {
        let instance = this.myToken;
        let totalSupply =await instance.totalSupply();
        let balanceOfDeployer=await instance.balanceOf(deployerAccount);
       // let balance=await instance.balanceOf(account[0]);
        // assert.equal(balance.valueOf(), initialSupply.valueOf(), "The balance was not the same");
        expect(await instance.balanceOf(deployerAccount)).to.be.a.bignumber.equal(totalSupply);
    })
    
    it("is possible to send token between accounts",async()=>{
        const sendTokens=1;
        let instance=this.myToken;
        let totalSupply =await instance.totalSupply();
        let balanceOfDeployer=await instance.balanceOf(deployerAccount);
        // console.log("total",totalSupply);
        // console.log("balance",balanceOfDeployer);
        // console.log("token",sendTokens);
        expect(new BN(balanceOfDeployer)).to.be.a.bignumber.eql(totalSupply);
        await expect(instance.transfer(recipient, 1)).to.eventually.be.fulfilled; 
        await expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(totalSupply.sub(new BN(sendTokens)));
        await expect(instance.balanceOf(recipient)).to.eventually.be.a.bignumber.equal(new BN(sendTokens));
    })

    it("is not possible to send more tokens than available in total", async()=>{
        let instance=this.myToken;
        let balanceOfDeployer=await instance.balanceOf(deployerAccount);
        expect(instance.transfer(recipient,new BN(balanceOfDeployer))).to.eventually.be.rejected;
        expect(await instance.balanceOf(deployerAccount)).to.be.a.bignumber.equal(balanceOfDeployer);
    })
    
    
});

Your Answer

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

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