Solidity

安全帽基本測試失敗

  • February 6, 2022

我在測試智能合約時遇到了一些問題,這些問題對我來說似乎非常好。我已經越來越多地減少了儲存庫,剩下的就是這個。

MappingContract.sol

pragma solidity = 0.8.11;

contract MappingContract{
   mapping(uint256 => uint256) internal identity;

   function addNumber(uint256 number) external{
       identity[number] = number;
   }

   function getIdentity(uint256 number) external returns (uint256 numberOut){
       numberOut = identity[number];
   }

   constructor() {
       identity[0] = 0;
   }
}

testMapping.spec.ts

const { waffle } = require('hardhat');
const { ethers } = require('hardhat');
const { expect } = require('chai');

const { Contract, Wallet, BigNumber } = ethers;
const { MockProvider, deployContract } = waffle;

const overrides = {
   gasLimit: 13000000
};

describe('MappingTests', () => {

   it('test', async () => {
       const signers = await ethers.getSigners();
       let owner = signers[0];

       let mAddress= await ethers.getContractFactory("MappingContract");

       let m = await mAddress.deploy();

       let result = await m.getIdentity(0);
       
   });
});

當我執行測試時,我收到以下錯誤

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (./test/testMapping.spec.ts)
     at listOnTimeout (internal/timers.js:554:17)
     at processTimers (internal/timers.js:497:7)

契約和測試對我來說似乎很好,它在使用 remix 執行時有效,但在使用安全帽執行時失敗。我是否使用了錯誤版本的安全帽或任何其他軟體包?我對鬆露也有類似的問題。這是我的配置文件

package.json

{
 "name": "MappingTest",
 "version": "1.1.0-beta.0",
 "description": "Mapping Tests",
 "engines": {
   "node": ">=10"
 },
 "files": [
   "build",
   "contracts"
 ],
 "dependencies": {},
 "devDependencies": {
   "@nomiclabs/hardhat-ethers": "^2.0.4",
   "@nomiclabs/hardhat-waffle": "^2.0.2",
   "chai": "^4.2.0",
   "ethereum-waffle": "^3.4.0",
   "ethers": "^5.5.4",
   "hardhat": "2.8.3",
   "mocha": "^6.2.2",
   "typescript": "^3.7.3"
 },
 "scripts": {
   "compile": "yarn hardhat compile",
   "test": "yarn mocha test/testMapping.spec.ts"
 },
 "license": "GPL-3.0-or-later"
}

hardhat.config.js

require('@nomiclabs/hardhat-ethers');
require('@nomiclabs/hardhat-waffle');

module.exports = {
 networks: {
     hardhat: {
         mining: {
             auto: false,
             interval: 5000,
         },
         blockGasLimit: 13000000,
         gasPrice: 20
     },
 },
 
 solidity: {
     compilers:[
         {
             version: "0.8.11"
         }
     ]
 }
};

您的安全帽設置為每 5 秒探勘一個塊(在您的安全帽配置中)。您的測試似乎在 2 秒後超時。顯然這些東西不能很好地協同工作。

我強烈建議你讓安全帽網路自動探勘區塊,除非你確定你只需要每隔一段時間才需要它來探勘。要啟用自動挖礦,只需刪除 block mining

引用自:https://ethereum.stackexchange.com/questions/120957