1

I can't get a test to pass, for me it is very clear but I am missing something obvious:

This is the smart contract:

function getContractOwner() public pure returns (string memory contractOwnerAddress) {
    return "0x67F4CfB03aA5E2E71De1470ae26adB7e33B7892E";
}

This is the test:

contract("Exams", async accounts => {
  it("should pass this test", async () => {
    assert.equal("0", "0", "LOGICAL!")
  });
  it("should keep the contract owner", async () => {

    let instance = await Exam.deployed();
    let address = instance.getContractOwner.call(accounts[0]);
    // console.log(address);
    assert.equal(address.valueOf(), "0x67F4CfB03aA5E2E71De1470ae26adB7e33B7892E", "the address is not correct");

  });
});

I get this error:

 Contract: Exams
    ✓ should pass this test
    1) should keep the contract owner
    > No events were emitted


  1 passing (74ms)   1 failing

  1) Contract: Exams
       should keep the contract owner:
     AssertionError: the address is not correct: expected { _events: {},   emit: [Function: emit],   on: [Function: on],   once: [Function: once],   off: [Function: removeListener],   listeners: [Function: listeners],   addListener: [Function: on],   removeListener: [Function: removeListener],   removeAllListeners: [Function: removeAllListeners] } to equal '0x67F4CfB03aA5E2E71De1470ae26adB7e33B7892E'
      at Context.<anonymous> (test/TestExams.js:15:15)
      at processTicksAndRejections (internal/process/task_queues.js:93:5)
2

You are doing the invocation wrong, try with the following:

 it("should keep the contract owner", async () => {
   let instance = await Example.deployed();
   let address = await instance.getContractOwner.call({from: accounts[0]});
   // console.log(address);
   assert.equal(address.valueOf(), "0x67F4CfB03aA5E2E71De1470ae26adB7e33B7892E", "the address is not correct");
 });

Read about using async/await from the Truffle docs.

Tested:

$ truffle test
Using network 'development'.

Compiling your contracts...
===========================
> Compiling ./contracts/Example.sol
> Artifacts written to /tmp/test-1191021-9679-1hbjgq3.xdq8
> Compiled successfully using:
   - solc: 0.5.8+commit.23d335f2.Emscripten.clang

  Contract: Example
    ✓ should pass this test
    ✓ should keep the contract owner (45ms)

  2 passing (116ms)
| improve this answer | |
  • Thanks, do you know where can I find the documentation for this? – Alejandro Veintimilla Nov 21 '19 at 19:59
  • @AlejandroVeintimilla just updated my answer with that ;). – alberto Nov 21 '19 at 20:00

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.