STA:scaffold-eth 挑战:测试覆盖率(Part3)_Restart Energy MWAT

译文出自:登链翻译计划

译者:翻译小组

校对:Tiny熊

我知道,你想直接部署合约和前端,并立刻就开始在测试网上进行测试,但是......我们需要确定一切都按预期工作,而不需要在前端用户界面上进行monkey测试。

因此,在文章的下一部分,我将介绍一些开发人员应该做的事情:测试合约逻辑!

Waffle

Waffle是一个用于编写和测试智能合约的库,它与ethers-js配合得非常默契。

Waffle有很多有帮助的工具。waffle中的测试是用Mocha和Chai一起编写的。你可以使用不同的测试环境,但Waffle的匹配器(matcher)只能在chai下工作。

我们将使用Chai匹配器来验证我们所期望的条件是否已经满足。

在写完所有的测试用例后,你只需要输入yarntest,就会自动针对你的合约进行测试。

我不会解释如何使用这个库,我将专注于应该测试什么。

我们的合约已经实现了一些逻辑:

用mapping(address=>uint256)publicbalances保存用户余额

有一个最小质押金额的阀值uint256publicconstantthreshold=1ether。

有一个最大的时间限制(deadline)uint256publicdeadline=block

);it('executerevertedbecauseexternalcontractalreadycompleted',async()=>{constamount=ethers

);it('executerevertedbecausedeadlineisreached',async()=>{//reachthedeadlineawaitincreaseWorldTimeInSeconds(180,true);awaitexpect(stakerContract

);it('externalcontractsucessfullycompleted',async()=>{constamount=ethers

);});

第一个测试:如果在质押金额没有达到阈值的情况下调用execute()函数,它将撤销交易并返回适当的错误信息。

第二个测试:连续两次调用execute()函数,质押已经完成,交易应该被撤销,防止再次调用。

第三个测试:在时间到deadline之后调用execute()函数。交易应该被撤销,因为只能在时间到deadline之前调用execute()函数。

最后一个测试:如果所有的要求都满足,那么execute()函数不会回退,并且所有都如预期一样。在函数调用外部合约后,completed变量应该是true,外部合约balance应该等于用户的质押金额,我们的合约余额应该等于0。

如果一切正常,运行yarntest应该会有这样的输出:

完整测试代码

下面我们来看看整个测试代码:

const{ethers}=require('hardhat');const{use,expect}=require('chai');const{solidity}=require('ethereum-waffle');use(solidity);//UtilitiesmethodsconstincreaseWorldTimeInSeconds=async(seconds,mine=false)=>{awaitethers

};describe('StakerdApp',()=>{letowner;letaddr1;letaddr2;letaddrs;letstakerContract;letexampleExternalContract;letExampleExternalContractFactory;beforeEach(async()=>{//DeployExampleExternalContractcontractExampleExternalContractFactory=awaitethers

);describe('Testcontractutilsmethods',()=>{it('timeLeft()return0afterdeadline',async()=>{awaitincreaseWorldTimeInSeconds(180,true);consttimeLeft=awaitstakerContract

);it('timeLeft()returncorrecttimeleftafter10seconds',async()=>{constsecondElapsed=10;consttimeLeftBefore=awaitstakerContract

);});describe('Teststake()method',()=>{it('Stakeeventemitted',async()=>{constamount=ethers

);it('Stake0

);it('Stakerevertedifdeadlineisreached',async()=>{//LetdeadlinebereachedawaitincreaseWorldTimeInSeconds(180,true);constamount=ethers

);it('Stakerevertedifexternalcontractiscompleted',async()=>{constamount=ethers

);});describe('Testexecute()method',()=>{it('executerevertedbecausestakeamountnotreachedthreshold',async()=>{awaitexpect(stakerContract

);it('executerevertedbecauseexternalcontractalreadycompleted',async()=>{constamount=ethers

);it('executerevertedbecausedeadlineisreached',async()=>{//reachthedeadlineawaitincreaseWorldTimeInSeconds(180,true);awaitexpect(stakerContract

);it('externalcontractsucessfullycompleted',async()=>{constamount=ethers

);});describe('Testwithdraw()method',()=>{it('Withdrawrevertedifdeadlineisnotreached',async()=>{awaitexpect(stakerContract

);it('Withdrawrevertedifexternalcontractiscompleted',async()=>{//CompletethestakeprocessconsttxStake=awaitstakerContract

);it('Withdrawrevertedifaddresshasnobalance',async()=>{//LettimepassawaitincreaseWorldTimeInSeconds(180,true);awaitexpect(stakerContract

);it('Withdrawsuccess!',async()=>{//Completethestakeprocessconstamount=ethers

);});});

你是否注意到,测试代码的覆盖率远远大于合约本身?这就是我们想看到的!测试所有的东西!

本翻译由CellETF赞助支持。

来源:https://stermi.medium.com/how-to-write-your-first-decentralized-app-scaffold-eth-challenge-1-staking-dapp-b0b6a6f4d242

参考资料

登链翻译计划:https://github.com/lbc-team/Pioneer

翻译小组:https://learnblockchain.cn/people/412

Tiny熊:https://learnblockchain.cn/people/15

Waffle:https://ethereum-waffle.readthedocs.io/en/latest/index.html

Mocha:https://mochajs.org/

Chai:https://www.chaijs.com/

Chai匹配器:https://ethereum-waffle.readthedocs.io/en/latest/matchers.html

CellETF:https://celletf.io/?utm_souce=learnblockchain

免责声明:作为区块链信息平台,本站所发布文章仅代表作者个人观点,与链闻ChainNews立场无关。文章内的信息、意见等均仅供参考,并非作为或被视为实际投资建议。

本文来源于非小号媒体平台:

登链社区

现已在非小号资讯平台发布105篇作品,

非小号开放平台欢迎币圈作者入驻

入驻指南:

/apply_guide/

本文网址:

/news/10481469.html

免责声明:

1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险

2.本文版权归属原作所有,仅代表作者本人观点,不代表非小号的观点或立场

上一篇:

比特币进入牛市交叉点,BTC反弹迫在眉睫?

郑重声明: 本文版权归原作者所有, 转载文章仅为传播更多信息之目的, 如作者信息标记有误, 请第一时间联系我们修改或删除, 多谢。

大币网

[0:0ms0-3:21ms