微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决摩卡合约测试错误:Done在挂钩中多次调用

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

-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(),'John Doe');
    });
});

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。