使用 Blueprint 编写测试
概览
测试工具包(通常是沙盒)已经包含在名为Blueprint的TypeScript SDK中。您可以创建一个演示项目并通过两个步骤启动默认测试:
- 创建一个新的Blueprint项目:
npm create ton@latest MyProject
- 运行测试:
cd MyProject
npx blueprint test
然后,您将在终端窗口中看到相应的输出:
% npx blueprint test
> MyProject@0.0.1 test
> jest
 PASS  tests/Main.spec.ts
  Main
    ✓ should deploy (127 ms)
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.224 s, estimated 2 s
Ran all test suites.
基本用法
测试智能合约可以涵盖安全性、优化Gas支出和检查极端情况。 在Blueprint(基于Sandbox)中编写测试是通过定义与合约的任意操作并将测试结果与预期结果进行比较来实现的,例如:
it('should execute with success', async () => {                                          // 测试用例的描述
    const res = await main.sendMessage(sender.getSender(), toNano('0.05')); // 执行合约main的操作并将结果保存在res中
    expect(res.transactions).toHaveTransaction({                           // 使用expect()函数配置预期结果
        from: main.address,                                                // 设置我们想要测试匹配属性的发送者
        success: true                                                      // 使用匹配属性success设置期望结果
    });
    printTransactionFees(res.transactions);                                // 打印有关花费的费用的详细信息
});
编写复杂Assertion的测试
创建测试的基本流程是:
- 使用blockchain.openContract()创建特定的wrappedContract实体。
- 描述您的Contract应执行的操作并将执行结果保存在res变量中。
- 使用expect()函数和匹配器toHaveTransaction()验证属性。
toHaveTransaction匹配器所期望的对象包含以下属性中的任意组合,这些属性来自FlatTransaction类型
| 名称 | 类型 | 描述 | 
|---|---|---|
| from | Address? | 消息发送者的合约地址 | 
| on | Address | 消息目的地的合约地址  (属性 to的替代名称)。 | 
| value | bigint? | 消息中Toncoin的数量,以nanotons计算 | 
| body | Cell | 定义为Cell的消息体 | 
| op | number? | op是操作标识符号码(通常为TL-B的crc32)。在消息体的前32位中。 | 
| success | boolean? | 自定义沙盒标志,定义特定交易的结果状态。如果计算和行动阶段都成功,则为True。否则为False。 | 
您可以省略您不感兴趣的字段,并传递接受类型并返回布尔值的函数(true表示可以),以检查例如数字范围、消息操作码等。请注意,如果字段是可选的(如from?: Address),那么函数也需要接受可选类型。
提示
您可以从Sandbox文档中查看所有匹配器字段的完整列表。
特定测试套件
提取发送模式
要提取发送消息的发送模式,您可以使用以下代码:
const smc = await blockchain.getContract(addr);
const re = blockchain.executor.runTransaction({
    config: blockchain.configBase64, libs: null, verbosity: 'full',
    now: Math. floor (Date.now) / 1000),
    lt: BigInt(Date.now()),
    randomSeed: null,
    ignoreChksig: false,
    debugEnabled: true,
    shardAccount: beginCell()
        .store (storeShardAccount (smc.account))
        .endCell()
        .toBoc()
        .toString('base64'),
    message: beginCell()
        .store (storeMessageRelaxed (...))
        .endCell(),
});
if (!re.result. success || !re.result.actions) {
    throw new Error('fail');
}
const actions = loadoutList(Cell.fromBase64(re.result.actions).beginParse());
actions[0].type === 'sendMsg' && actions[0].mode;
教程
从TON社区最有价值的教程中了解更多关于测试的信息:
- 第2课:为智能合约测试FunC
- TON Hello World第4部分:逐步指导测试您的第一个智能合约
- TON智能合约传递途径
- [YouTube]第六课 FunC & Blueprint。Gas,费用,测试。
示例
查看用于TON生态系统合约的测试套件,并通过示例进行学习。
- liquid-staking-contract沙盒测试
- governance_tests
- JettonWallet.spec.ts
- governance_tests
- MassSender.spec.ts
- TonForwarder.spec.ts
- Assurer.spec.ts