使用 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 () => {                              // description of the test case
    const res = await main.sendMessage(sender.getSender(), toNano('0.05'));  // performing an action with contract main and saving result in res
    expect(res.transactions).toHaveTransaction({                             // configure the expected result with expect() function
        from: main.address,                                                  // set expected sender for transaction we want to test matcher properties from
        success: true                                                        // set the desirable result using matcher property success
    });
    printTransactionFees(res.transactions);                                  // print table with details on spent fees
});