【问题标题】:How to directly test a Solidity Library with Hardhat/Chai如何使用 Hardhat/Chai 直接测试 Solidity 库
【发布时间】:2022-08-08 01:21:35
【问题描述】:
我正在尝试使用安全帽和 chaï 直接测试 Solidity 库。
这是我想测试的库示例:
library LibTest {
function testFunc() public view returns(bool) {
return true;
}
}
这就是我试图测试它的方式。
beforeEach(async () => {
const LibTest = await ethers.getContractFactory(\"LibTest\");
const libTest = await LibTest.deploy();
await libTest.deployed();
})
describe(\'Testing test()\', function () {
it(\"is working testFunc ?\", async function () {
console.log(await libTest.testFunc());
})
})
但我有错误信息:
ReferenceError:未定义 libTest
我阅读了Chai doc 和Hardhat doc 上的所有内容,但找不到任何解决方案
标签:
solidity
chai
hardhat
【解决方案1】:
我发现解决此问题的最佳方法是创建一个调用和测试 Lib 本身的 LibTest.sol 合约。并且只是在 JS/TS 中运行抽象测试来调用 LibTest 合约,在 Hardhat 中部署期间将 Lib.sol 合约连接到它。
const Lib = await ethers.getContractFactory("Lib");
const lib = await Lib.deploy();
const LibTest = await ethers.getContractFactory("LibTest", {
libraries: {
Lib: lib.address,
},
});
const libTest = await LibTest.deploy();
/// later: console.log(await libTest.testLibFunc());
LibTest.sol:
import "./Lib.sol";
library LibTest {
function testLibFunc() public view returns(bool) {
bool response = Lib.testFunc();
return response;
}
}
你能找到更好的方法吗?