我在我的智能合同(AssemblyScript)中有一个函数是我想要测试的。我想测试这个断言是否真的发生了。
AssemblyScript
foo(id: string): boolean {
assert(id != 'bar', 'foo cannot be bar');
return true;
}单元测试(如:)
describe('Contract', () => {
it('should assert', () => {
contract.foo('bar'); // <-- How to test assertion here
})
});运行上述测试后,控制台日志显示
失败:应该断言- foo不能是bar
我知道我可以返回false或throw,而不是对上面的示例执行assert,如果这样做使测试更容易,我可能会这样做。
发布于 2022-07-01 17:52:49
使用toThrow()
如下所示:
describe('Contract', () => {
it('should assert', () => {
contract.foo('bar').toThrow('foo cannot be bar');
})
});您还可以使用not.toThrow()测试而不是对流层:
it('should assert', () => {
contract.foo('foo').not.toThrow();
})
});https://stackoverflow.com/questions/72241251
复制相似问题