0

I have this tests that supposedly pass (in the terminal, I can see the checks). But they never actually run the code:

it("should add an exam to the exams list", () => {
        let hash_test = "9F86D081884C7D659A2FEAA0C55AD015A3BF4F1B2B0B822CD15D6C15B0F00A08";
        Exam.deployed()
            .then(instance => instance.addExam(hash_test, {from: accounts[0]}))
            .then(hash => {
                assert.equal(hash.valueOf(), hash_test, "Not returning the correct address");
                console.log("THIS");
            })
            .then(instance => instance.isOwner(hash_test, {from: accounts[0]}))
            .then(isOwner => {
                assert.equal(isOwner, "true", "Not recognizing hash correctly");
                assert.equal("true", "false", "NOT WORKING AT ALL");
            })
            .then(instance => instance.isOwner("THISISWRONG", {from: accounts[0]}))
            .then(isOwner => {
                assert.equal(isOwner, "false", "Not recognizing hash correctly")
            });
    });

As you can see, there is an assert.equal("true", "false" . But I still get the checks as if it passed:

Compiling your contracts...

Compiling ./contracts/Exams.sol

Contract: Exams ✓ should pass this test ✓ should add an exam to the exams list ✓ Should add an student address to the people that passed the exam

3 passing (68ms)

I don't know what could be wrong, my code is almost the same as the one in the official site.

1

Since this part doesn't return anything:

.then(hash => {
    assert.equal(hash.valueOf(), hash_test, "Not returning the correct address");
    console.log("THIS");
})

No other then is executed passed this point, and the it completes successfully.

I'm assuming that you have two additional its in your test, hence the printout 3 passing.

Aside from that, I recommend using the async/await scheme, which yields a much cleaner code while achieving the exact same goal:

it("should add an exam to the exams list", async function() {
    const hash_test = "9F86D081884C7D659A2FEAA0C55AD015A3BF4F1B2B0B822CD15D6C15B0F00A08";
    const instance = await Exam.deployed(); // maybe `await` isn't needed here
    const hash = await instance.addExam(hash_test, {from: accounts[0]});
    assert.equal(hash.valueOf(), hash_test, "Not returning the correct address");
    const isOwner = await instance.isOwner(hash_test, {from: accounts[0]});
    assert.equal(isOwner, true, "Not recognizing hash correctly");
    const isNotOwner = await instance.isOwner("THISISWRONG", {from: accounts[0]});
    assert.equal(isNotOwner, false, "Not recognizing hash correctly")
});

Note that isOwner should be compared to true/false and not to "true"/"false", because web3.js returns Boolean values as Boolean and not as String.

Also, as you've already been explained on several previous questions of yours, because function addExam changes the state of the contract on the blockchain, when you call it from the off-chain (e.g., a web3.js script), you get back a receipt object, and not the actual value which your function returns (this one is available only when you call the function from the on-chain, i.e., from this contract or from another contract).

Thus, the statement assert.equal(hash.valueOf(), hash_test) is prone to failure.

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.