0

I have a js truffle test:

it("should add an exam to the exams list", async () => {
    // This should test the complete functionality
    let hash_test = "fB03aA5E2E71De1470ae2";
    let instance = await Exam.deployed();
    let hash = instance.addExam.call({from: accounts[0], hash: hash_test});
    assert.equal(hash.valueOf(), hash_test, "Not returning the correct address")
})

And I have an addExam function in the contract:

function addExam(string memory hash) public returns (string memory examProfessorHash) {
    // save the exam hash and link it with the professors address
    professorsExam[msg.sender] = hash;

    return hash;
}

When I run the tests, I get this error:

Error: invalid string value (arg="hash", coderType="string", value={"from":"0x67F4CfB03aA5E2E71De1470ae26adB7e33B7892E","hash":"fB03aA5E2E71De1470ae2"}) at PromiEvent

I am not sure how to debug this. Is it the .call() method?

  • addExam(hash_test).send() I think it should be send not call so try this first or see a truffle example – Majd TL Nov 21 '19 at 20:45
  • There is no offical truffle example with an argument being passed to the call() function, at least not in the trufflesuite.com/docs/truffle/testing/… docs – Alejandro Veintimilla Nov 21 '19 at 20:57
  • You are sending a transaction to the blockchain, you don't need a call method , you need a send – Majd TL Nov 21 '19 at 21:02
  • I would love to find real docs about this. There is no reference to a send method. – Alejandro Veintimilla Nov 21 '19 at 21:06
  • instance.addExam(hash_test).send({from: accounts[0]}) doesn't work. TypeError: instance.addExam(...).send is not a function – Alejandro Veintimilla Nov 21 '19 at 21:19
1

Your issue here is that your call is not passing hash as a parameter, but rather as an option to Truffle. In reference to this line:

let hash = instance.addExam.call({from: accounts[0], hash: hash_test});

The options provided inside curly brackets do not go on to the function call, but are passed to Truffle. Also, Solidity doesn't support named arguments, only positional arguments. Function parameters can be passed directly in the call parenthesis:

let hash = instance.addExam.call(hash_test, {from: accounts[0]});

Side note

Your code will not actually add an exam to the list. call signifies that the transaction will do a "dry run" of the transaction, and return what it would return if you were to actually send the transaction. It does not actually make any lasting updates to the blockchain state, nor does it cost gas from the sender. If you would like to update your call to a transaction (which does cost gas and update blockchain state), change your code to this:

let hash = instance.addExam(hash_test, {from: accounts[0]});

For more details on call vs transaction, see this fantastic answer: What is the difference between a transaction and a call?

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.