为什么我在 Solidity 智能合约测试中收到 AssertionError 错误?

时间:2021-07-17 07:23:47

标签: javascript solidity

test.js 文件
我试图对 Contracr 进行一些测试,但收到了 AssertionError 错误,下面是我编写的代码。

const provider = ganache.provider();
const web3 = new Web3(provider);
const {interface,bytecode} = require('../compile');
let accounts;
let inbox;
beforeEach(async() => {
  
  accounts =await web3.eth.getAccounts();
  
  inbox = await new web3.eth.Contract(JSON.parse(interface))
      .deploy({
        data: bytecode,
        arguments: ['Hi there!']
      })
      .send({ from: accounts[0], gas: '1000000' });
  inbox.setProvider(provider);
});
describe('inbox',()=>{
  it('deploys',()=>{
      assert.ok(inbox.options.address);
  });
  it('has some default message', async () =>{
    const message = await inbox.methods.message().call();
    assert.equal(message, 'Hi there');
  });
});

我从终端得到的输出

我收到的错误是

  1 passing (615ms)
  1 failing

  1) inbox
       has some default message:

      AssertionError [ERR_ASSERTION]: '' == 'Hi there'
      + expected - actual

      +Hi there
      
      at Context.it (test/Inbox.test.js:28:12)
      at process._tickCallback (internal/process/next_tick.js:68:7)

compile.js 文件

这是我编写的用于编译我的 contrace 的代码。

 const path = require('path');
 const fs = require('fs');
 const solc = require('solc');
    
 const inboxPath = path.resolve(__dirname,'contracts','inbox.sol');
 const source = fs.readFileSync(inboxPath,'utf8');
    
 module.exports = solc.compile(source,1).contracts[':Index'];

inbox.sol 文件

这是我的合同。

//SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.4.25;
contract Index{
    string public message;
    function Inbox(string  _reqstring) public {
        message = _reqstring;
    }
    function setMess(string  _init) public {
        message = _init;
    }

}

0 个答案:

没有答案