@1hive/contract-helpers-test 中文文档教程
Aragon test helpers
通用 Solidity 和 Aragon 相关智能合约的测试助手。
??? 此包旨在用于
web3.js@1
和 Truffle v5 环境。 如果您使用的是 ethers.js,waffle 大多数断言匹配器都包含电池会需要。
Quick start
使用 yarn add --dev @1hive/contract-helpers-test
安装。
在您的测试中,您将像这样导入这些实用程序:
const {
bn,
getEvents,
MAX_UINT256,
ZERO_ADDRESS,
} = require('@1hive/contract-helpers-test')
const {
assertEvent,
assertRevert,
} = require('@1hive/contract-helpers-test/asserts')
const {
newDao,
newDaoFactory,
} = require('@1hive/contract-helpers-test/aragon-os')
许多合约模拟也可以通过 @1hive/contract-helpers-test/contracts/
访问,并且可能对测试 Aragon 应用程序或其他通用智能合约。 contracts/
子目录中提供了有关这些合同模拟的更多文档。
API
分为几个主要导出:
@1hive/contract-helpers-test/
: generic utilities for on-chain interactions@1hive/contract-helpers-test/asserts
: custom assertions for on-chain interactions@1hive/contract-helpers-test/aragon-os
: aragonOS-related utilities@1hive/contract-helpers-test/aragon-os/asserts
: aragonOS-related assertions
此外,该包还附带:
@1hive/contract-helpers-test/contracts
: smart contract mocks@1hive/contract-helpers-test/scripts
: utility shell scripts
Global state
测试助手 /代码>)。
但是,如果文件开头不存在绑定,则可以覆盖此默认绑定或注入您自己的绑定。
ctx
包含尾随 ctx
参数的各个实用程序允许您专门为该单个调用覆盖环境。
type Artifacts = {
require: (name: string) => TruffleContract
}
type Context = {
artifacts: Artifacts
web3: Web3
}
injectArtifacts()
将 artifacts
对象注入到全局默认环境中。
请注意,所有未来的调用现在都将默认引用这个新的 artifacts
对象。
injectWeb3()
将 web3
对象注入全局默认环境。
请注意,所有未来的调用现在都将默认引用这个新的 web3
对象。
Generic utilities
Constants
以下常量可用,且大部分不言自明:
- Ethereum related
EMPTY_BYTES
ZERO_ADDRESS
ZERO_BYTES32
- Number related
MAX_UINT64
MAX_UINT192
MAX_UINT256
- Time related
NOW
:Date.now()
, in secondsTOMORROW
: one day ahead ofDate.now()
, in secondsNEXT_WEEK
: one week ahead ofDate.now()
, in secondsONE_DAY
: duration for one day, in secondsONE_WEEK
: duration for one week, in seconds
Custom assertions
aragonOS utilities
Constants
以下常量可用:
- ACL
ANY_ENTITY
: address denoting any address has permissionBURN_MANAGER
: address denoting a permission has been frozen via discarding (burning) its manager- EVM Scripts
EMPTY_CALLS_SCRIPT
: empty CallsScript (with spec id 1)
aragonOS assertions
Smart contract mocks
请访问 contracts/
中提供的文档。
Shell scripts
ganache-cli.sh
用于启动本地 ganache-cli
或 solidity-coverage
RPC 节点并启动 npx truffle 测试
的实用程序脚本。
此脚本通常被认为已弃用,但如果您的测试框架仍需要您手动启动本地测试节点,则它可能很有用。
如果您还没有这样做,您可能有兴趣迁移到 buidler,它会为您处理此连接。
Aragon test helpers
Test helpers for both generic Solidity and Aragon-related smart contracts.
???? This package is designed to be used in a
web3.js@1
and Truffle v5 environment. If you are using ethers.js, you are likely much better served by waffle which comes batteries-included with most of the assertion matchers you'll need.
Quick start
Install with yarn add --dev @1hive/contract-helpers-test
.
In your tests, you'll then import these utilities like so:
const {
bn,
getEvents,
MAX_UINT256,
ZERO_ADDRESS,
} = require('@1hive/contract-helpers-test')
const {
assertEvent,
assertRevert,
} = require('@1hive/contract-helpers-test/asserts')
const {
newDao,
newDaoFactory,
} = require('@1hive/contract-helpers-test/aragon-os')
A number of contract mocks are also accessible through @1hive/contract-helpers-test/contracts/
and may be useful for testing Aragon apps or other generic smart contracts. More documentation on these contract mocks is available in the contracts/
subdirectory.
API
The test helpers are grouped into several main exports:
@1hive/contract-helpers-test/
: generic utilities for on-chain interactions@1hive/contract-helpers-test/asserts
: custom assertions for on-chain interactions@1hive/contract-helpers-test/aragon-os
: aragonOS-related utilities@1hive/contract-helpers-test/aragon-os/asserts
: aragonOS-related assertions
Furthermore, the package also comes with:
@1hive/contract-helpers-test/contracts
: smart contract mocks@1hive/contract-helpers-test/scripts
: utility shell scripts
Global state
The utilities exposed from the test helpers will automatically detect and bind to an injected Truffle environment (artifacts
and web3
).
However, it is possible to override this default binding or inject your own if none exist at the start of a file.
ctx
Individual utilities containing a trailing ctx
parameter allow you to override the environment specifically for that single call.
type Artifacts = {
require: (name: string) => TruffleContract
}
type Context = {
artifacts: Artifacts
web3: Web3
}
injectArtifacts()
Inject an artifacts
object into the global default environment.
Note that all future calls will now reference this new artifacts
object by default.
injectWeb3()
Inject a web3
object into the global default environment.
Note that all future calls will now reference this new web3
object by default.
Generic utilities
Constants
The following constants are available, and mostly self-explanatory:
- Ethereum related
EMPTY_BYTES
ZERO_ADDRESS
ZERO_BYTES32
- Number related
MAX_UINT64
MAX_UINT192
MAX_UINT256
- Time related
NOW
:Date.now()
, in secondsTOMORROW
: one day ahead ofDate.now()
, in secondsNEXT_WEEK
: one week ahead ofDate.now()
, in secondsONE_DAY
: duration for one day, in secondsONE_WEEK
: duration for one week, in seconds
Custom assertions
aragonOS utilities
Constants
The following constants are available:
- ACL
ANY_ENTITY
: address denoting any address has permissionBURN_MANAGER
: address denoting a permission has been frozen via discarding (burning) its manager- EVM Scripts
EMPTY_CALLS_SCRIPT
: empty CallsScript (with spec id 1)
aragonOS assertions
Smart contract mocks
Please visit the documentation available in contracts/
.
Shell scripts
ganache-cli.sh
A utility script to start a local ganache-cli
or solidity-coverage
RPC node and initiate npx truffle test
.
This script is generally considered deprecated but may be useful if your test framework still requires you to start a local test node manually.
If you haven't already done so, you may be interested in migrating to buidler, which handles this connection for you.