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

时间:2020-06-26 10:33:14

标签: javascript mocha solidity

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

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);
 });
});

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

1 个答案:

答案 0 :(得分:1)

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