摩卡合约测试错误:Done()在挂钩中多次调用

时间:2020-11-09 16:03:59

标签: javascript mocha smartcontracts

嗨,我是智能合约领域的新手,并且我遇到了与摩卡有关的问题。这是我正在从事的测试:

-EDIT-

const UsersContract = artifacts.require("../contracts/UsersContract.sol");

contract("TestUsersContract", async accounts => {
    let instance;

    beforeEach('setup contract for each test', async () => {
        instance = await UsersContract.deployed();
    });

    it('should retrieve User', async () => {
        instance.join(1, "John Doe");
        instance.send({ from: accounts[0], gas: 50000 });

        let user = await instance.getUser(accounts[0]);
        assert.equal(user[0].toNumber(), 1);
        assert.equal(user[1], 'John Doe');
    });
});

这引发了以下错误:

    1) Uncaught Error: the string "abort(Error: Given input \"[object Object]\" is not a number.). Build with -s ASSERTIONS=1 for more info." was thrown, throw an Error :)
    2) Error: done() called multiple times in hook

我不知道为什么,但是当我将测试重构为非异步版本时:

it('should retrieve User', () => {
    instance.join(1, "John Doe");
    instance.send({ from: accounts[0], gas: 50000 });

    instance.getUser(accounts[0]).then( user => {
        assert.equal(user[0].toNumber(), 1);
        assert.equal(user[1], 'John Doe');
    });
});

一切顺利,测试按预期进行。为什么会这样呢?可以在这里完成async/await版本吗?还是我必须始终坚持使用.then()版本?

有点启发是很好的,谢谢。

0 个答案:

没有答案