0

I am testing my smart contract in truffle. I need to declare some global variables hence I did so outside 'it'.

I also tried declaring a global contract instance that I can use in all test cases. Though when I do so, none of the test cases are executed.

Consider this test script:

const MFT= artifacts.require('MFT');

contract("MFT", async (accounts) => {


let instance = await MFT.deployed();
let owner = accounts[0];

it("should be deployed by owner", async () => {

    let _onwer = await instance.getOwner.call();
    assert.equal(_onwer , owner);
});

});

The output that I get is:

> Compiled successfully using:
   - solc: 0.5.0+commit.1d4f565a.Emscripten.clang



  0 passing (1ms)

But when i do not initialize my instance outside the test case and do that within the test case, it works!

Consider the following test script:

const MFT= artifacts.require('MFT');

contract("MFT", async (accounts) => {


let instance;
let owner = accounts[0];

it("should be deployed by owner", async () => {
    instance = = await MFT.deployed();
    let _onwer = await instance.getOwner.call();
    assert.equal(_onwer , owner);
});

});

When I run this, i get the following output:

> Compiled successfully using:
   - solc: 0.5.0+commit.1d4f565a.Emscripten.clang



  Contract: MFT
    √ should be deployed by owner (43ms)


  1 passing (87ms)

What am i missing?

  • In your first example (the one which doesn't work) - the symbol UBBI is not defined anywhere. What exactly were you expecting to happen or hoping to achieve here??? – goodvibration May 28 at 15:20
  • 1
    BTW, contract takes and executes a function, not an async function, so you may as well get rid of that async up there. – goodvibration May 28 at 15:21
  • It can. Truffle documentation has it. – Kashish Khullar May 28 at 19:18
0

To initialize something before running any test you can use before(). You pass a function that it will execute previously to any test.

contract('MFT', (accounts) => {
    let instance;
    let owner;

    before(async() => {
        instance = await MFT.deployed();
        owner = accounts[0];
    });

    it("should be deployed by owner", async () => {
        let _onwer = await instance.getOwner.call();
        assert.equal(_onwer , owner);
    });
})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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