简体   繁体   English

如何测试 function 是否在 NEAR 智能合约(AssemblyScript)中断言?

[英]How to test if a function asserts in a NEAR smart contract (AssemblyScript)?

I have a function in my NEAR smart-contract (AssemblyScript) that I want to test.我的 NEAR 智能合约(AssemblyScript)中有一个 function 我想测试。 I want to test if the assertion actually happened.我想测试断言是否真的发生了。

AssemblyScript汇编脚本

foo(id: string): boolean {
  assert(id != 'bar', 'foo cannot be bar');
  return true;
}

Unit test ( as-pect )单元测试( 方面

describe('Contract', () => {
  it('should assert', () => {
    contract.foo('bar'); // <-- How to test assertion here
  })
});

After running the above test, the console logs says运行上述测试后,控制台日志显示

Failed: should assert - foo cannot be bar失败:应该断言 - foo 不能是 bar

I know I can return false or throw instead of doing an assert for the above example, and I may do that instead if it makes testing easier.我知道我可以返回falsethrow而不是对上面的示例进行assert ,如果它使测试更容易,我可以这样做。

use toThrow()使用toThrow()

Like this:像这样:

describe('Contract', () => {
  it('should assert', () => {
    contract.foo('bar').toThrow('foo cannot be bar');
  })
});

you can also use not.toThrow() to test not trowing:您还可以使用not.toThrow()来测试不投掷:

  it('should assert', () => {
    contract.foo('foo').not.toThrow();
  })
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 AssemblyScript 中为 NEAR 合同创建 UID? - How to create a UID in AssemblyScript for a NEAR contract? 为什么我的 NEAR 智能合约函数不返回对象(AssemblyScript)? - Why doesn't my NEAR smart-contract function return an object (AssemblyScript)? 使用 u128.add() function 在汇编脚本智能合约中添加 NEAR 令牌的问题 - Issues adding NEAR tokens in assemblyscript smart contract using u128.add() function 如何知道 NEAR 智能合约部署到哪个 environment.network (AssemblyScript)? - How to know which environment/network a NEAR smart-contract is deployed to (AssemblyScript)? 为什么我看到错误:在构建 NEAR 智能合约(AssemblyScript)时找不到名称“解码”和“编码”? - Why am I seeing error: Cannot find name 'decode' and 'encode' when building a NEAR smart contract (AssemblyScript)? 如何使用 AssemblyScript 在 NEAR 合约上使用 PersistentUnorderedMap? PersistentUnorderedMap 初始化后没有出现在合约存储中 - How to use PersistentUnorderedMap on NEAR contract using AssemblyScript? PersistentUnorderedMap does not appear in Contract Storage after init 从 Near 区块链调用 Aurora 合约时如何在 AssemblyScript 中编码参数? - How to encode arguments in AssemblyScript when calling Aurora contract from Near blockchain? 如何在NEAR智能合约中设置static字段? - How to set a static field in a NEAR smart contract? NEAR智能合约数据如何备份? - How to backup data of NEAR smart contract? 近距离协议,智能合约 - Near Protocol, Smart Contract
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM