0

Working through some tests in Truffle to become more acquainted with the environment but am running into an issue.

What I am trying to do

Test a smart contract with, among others, the following functions:

// Register an item using an identifier type (no., PID, barcode, etc.), the actual identifier/code, and a description
function registerItem(string memory _identifier, string memory _code, string memory _description) public {
    // Make sure the item does not already exists/is not already registered
    require(!itemExists[keccak256(abi.encodePacked(_identifier,_code))]);
    // Create an Item struct in memory that stores the item data
    Item memory newItem;
    newItem.identifier = _identifier;
    newItem.code = _code;
    newItem.description = _description;
    // Map hash of item details to true/bring item into existence
    itemExists[keccak256(abi.encodePacked(_identifier,_code))] = true;
    // Increment tokenCounter;
    tokenCounter ++;
    // Set unique tokenId using tokenCounter
    uint256 tokenId = tokenCounter;
    // Map the tokenId to the item
    tokenIdtoItem[tokenId] = newItem;
    // Mint the token using ERC721 mint method, which checks if the token is already owned and if not sets it to the new owner
    ERC721Token.mint(tokenId);
    //Emit event
    emit ItemRegistered(tokenId, _identifier, _code);
}

// Get item details by providing a tokenId
function getItemByToken(uint256 _tokenId) public view returns (string memory identifier, string memory code, string memory description) {
    Item memory returnItem = tokenIdtoItem[_tokenId];
    return(returnItem.identifier,returnItem.code,returnItem.description);
}

I am testing it with:

const assetTracker = artifacts.require("AssetTracker");

contract("AssetTracker contract test", async accounts => {
  it("should register an item", async () => {
    let instance = await assetTracker.deployed();
    await instance.registerItem("Barcode", "13456-fgs", "This is the first dummy item.", { from: accounts[0]});
    assert.equal(instance.getItemByToken(1), ["Barcode", "13456-fgs", "This is the first dummy item."]);
    // assert.equal(instance.ownerOf(1), accounts[0], "token is not owned by any or the correct user");
  });

});

What I am getting when testing in Truffle console on a ganache client:

 Contract: AssetTracker contract test
    1) should register an item

    Events emitted during test:
    ---------------------------

    Transfer(_from: <indexed>, _to: <indexed>, _tokenId: <indexed>)
    ItemRegistered(_tokenId: <indexed>, _identifier: <indexed>, _code: <indexed>)

    ---------------------------


  0 passing (237ms)
  1 failing

  1) Contract: AssetTracker contract test
       should register an item:
     AssertionError: expected { domain:
   { domain: null,
     _events:
      { removeListener: [Function: updateExceptionCapture],
        newListener: [Function: updateExceptionCapture],
        error: [Function: debugDomainError] },
     _eventsCount: 3,
     _maxListeners: undefined,
     members: [],
     _errorHandler: [Function],
     enter: [Function],
     exit: [Function],
     add: [Function],
     remove: [Function],
     run: [Function],
     intercept: [Function],
     bind: [Function],
     setMaxListeners: [Function: setMaxListeners],
     getMaxListeners: [Function: getMaxListeners],
     emit: [Function],
     addListener: [Function: addListener],
     on: [Function: addListener],
     prependListener: [Function: prependListener],
     once: [Function: once],
     prependOnceListener: [Function: prependOnceListener],
     removeListener: [Function: removeListener],
     off: [Function: removeListener],
     removeAllListeners: [Function: removeAllListeners],
     listeners: [Function: listeners],
     rawListeners: [Function: rawListeners],
     listenerCount: [Function: listenerCount],
     eventNames: [Function: eventNames] } } to equal [ Array(3) ]
      at Context.it (test\assettracker.js:7:12)
      at process._tickCallback (internal/process/next_tick.js:68:7)



1

Any ideas on what is happening? When I run the commands lose in the console (e.g. migrating, connecting to contract, executing the functions) everything works well.

Thanks in advance. Take care!

0

you should be calling the contract method with await as given below.

const assetTracker = artifacts.require("AssetTracker");

contract("AssetTracker contract test", async accounts => {
  it("should register an item", async () => {
    let instance = await assetTracker.deployed();
    await instance.registerItem("Barcode", "13456-fgs", "This is the first dummy item.", { from: accounts[0]});
    assert.equal(await instance.getItemByToken(1), ["Barcode", "13456-fgs", "This is the first dummy item."]);
    // assert.equal(instance.ownerOf(1), accounts[0], "token is not owned by any or the correct user");
  });

});

You should also consider using the beforeEach methods to reconstruct the instance without retaining the state across various test methods.

let instance;
beforeEach('setup contract instance for each test case execution', async () => {
    instance = await assetTracker.new();
});

and use as given below,

it("should register an item", async () => {
    await instance.registerItem("Barcode", "13456-fgs", "This is the first dummy item.", { from: accounts[0]});
    assert.equal(await instance.getItemByToken(1), ["Barcode", "13456-fgs", "This is the first dummy item."]);
    // assert.equal(instance.ownerOf(1), accounts[0], "token is not owned by any or the correct user");
  });

Hope this helps.

  • Totally forgot to add the await inside the assert function. Thanks for pointing it out! Happy holidays! – Valentijn van den Hout Dec 24 '18 at 8:32
  • Happy Holidays and wish you a very happy and prosperous New Year 2019 – Saravanan Dec 24 '18 at 16:58
  • Same to you! Have a great 2019! – Valentijn van den Hout Dec 26 '18 at 17:33

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.