[英]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.我知道我可以返回
false
或throw
而不是对上面的示例进行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.