Back

blockchain - solidity - foundry 4.单元测试 (test, console ) 与 console2

发布时间: 2024-04-01 23:51:00

refer to:
https://book.getfoundry.sh/forge/writing-tests

https://book.getfoundry.sh/forge/tests

一图查看:

运行:forge test

运行某个contract : forge test --match-contract YourTestContractName -vv

对于console的使用:https://book.getfoundry.sh/reference/forge-std/console-log.html

1. 可以输出4种数据类型:string, int, bool, address , 其他类型不支持。会报错(见下面)

例如:

console.log("after,owner: %s", yular2.owner());

console.log("yular contract address: %s", address(yular2));

2. 在test中使用时,需要用 forge test -vv 来显示,默认的命令不显示log

另外,使用 -vv的好处是: 可以 对assertEq 两侧的值做对比。

3. 报错:Member "log" not found or not visible after argument-dependent lookup in type(library console).

console2

可以使用console2来代替console, (因为后者需要判断各种类型,%s, %d 等,前者就是js console的风格)

import "forge-std/console2.sol";

17 address lastCaller = blockNumber.lastCaller();
18 console2.log("== lastCaller: ", lastCaller);

特别好用。

Back