测试智能合约时 Mocha 断言错误

Assertion error in Mocha while testing the smart contract

我正在测试我的以太坊智能合约并收到一个断言错误,因为其中一个测试用例失败了。

Contract: Market
✓ contract is deployed

  1 passing (142ms)
  1 failing

  1) Contract: Market
   offer added:
   AssertionError: Unspecified AssertionError

代码如下:

contract('Market', ()=>{
it('contract is deployed',async ()=>{
    const market = await Market.deployed();
    assert(market.address!='');
    })
it('offer added', async () =>{
    const market = await Market.deployed();    
    let oldVal = await market.numOffers();
    market.supplyRequest(1,10,1);
    let newVal = await market.numOffers();
    console.log("new value is "+newVal); //this prints 1 on console
    console.log("old value is "+oldVal); // this print 0 on console
    assert(oldVal+1==newVal);
 });
});

我不确定为什么即使值相等,测试用例也会失败。

您的 numOffers 可能返回一个字符串而不是一个数字,这就是为什么断言中的 +1 将值从“1”更改为“11”并且断言失败的原因。