简体   繁体   English

Solidity, Mocha AssertionError: Unspecified AssertionError

[英]Solidity, Mocha AssertionError: Unspecified AssertionError

I'm learning how to unit test solidity code with mocha, and I get this error from simple code:我正在学习如何使用 mocha 对可靠性代码进行单元测试,我从简单的代码中得到了这个错误:

contract hello{
    function returnString() public pure returns(string memory){
        return "this";
    }
}

When testing without try/catch:在没有 try/catch 的情况下进行测试时:

const Hello = artifacts.require('hello');

contract('hello', () => {

    it('should return string', async () => {
        const hello = await Hello.deployed();
        const result = hello.returnString();
        assert(result == "this");
    });
});

I get this error:我收到此错误:

  Contract: hello
    1) should return string
    > No events were emitted


  0 passing (291ms)
  1 failing

  1) Contract: hello
       should return string:
     AssertionError: Unspecified AssertionError
      at Context.<anonymous> (test/Hello.js:15:3)
      at processTicksAndRejections (node:internal/process/task_queues:94:5)

But when I'm testing it with try/catch":但是当我用 try/catch 测试它时:

const Hello = artifacts.require('hello');

contract('hello', () => {
    it('should return string', async () => {
        const hello = await Hello.deployed();
        const result = hello.returnString();
        
        try { 
            assert(result === "this");
        }
        catch (e){
            console.log(e);
        }
        
    });
});

I get this one: ( ✓ should return string is green, meaning it passed test)我得到了这个:( ✓ should return string是绿色的,这意味着它通过了测试)

  Contract: hello
AssertionError: Unspecified AssertionError
    at Context.<anonymous> (/home/kaneda/Documents/learn/testing/contract_1/test/Hello.js:18:4)
    at processTicksAndRejections (node:internal/process/task_queues:94:5) {
  showDiff: false,
  actual: null,
  expected: undefined
}
    ✓ should return string


  1 passing (143ms)

So, what is this behaviour, is it actually passing test but throwing error or what?那么,这种行为是什么,它实际上是通过测试但抛出错误还是什么? I'm a bit confused.我有点困惑。

Oh, silly mistake, I forgot await before hello.returnString() in哦,愚蠢的错误,我忘了在hello.returnString()之前await

const result = hello.returnString();

This explains why code was failing, but I still don't understand that try/catch behavior.这解释了为什么代码失败了,但我仍然不明白 try/catch 行为。

On the try catch error, you can see the error right above it "AssertionError: Unspecified AssertionError".在 try catch 错误中,您可以在其正上方看到错误“AssertionError: Unspecified AssertionError”。

It shows a green tick, because since you caught the error, for the testing you were actually succesfull.它显示一个绿色勾号,因为您发现了错误,因此您实际上已经成功进行了测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM