DAPP开发【04】测试驱动开发

测试驱动开发(Test Driven Development),是一种不同于传统软件开发流程的新型的开发方法。它要求在编写某个功能的代码之前先编写测试代码,然后只编写使测试通过的功能代码通过测试来推动整个开发的进行。这有助于编写简洁可用和高质量的代码,并加速开发过程
测试驱动开发是一种敏捷软件开发方法,它强调在编写功能代码之前先编写测试代码。这些测试代码描述了预期的功能行为,并且在开始编写实际功能代码之前会失败。然后,开发人员会专注于编写足够的功能代码,以使测试通过。这个过程被称为"红-绿-重构"(Red-Green-Refactor)循环:

红(Red):编写一个新的测试,期望某个功能,但该测试当前会失败(红色)。
绿(Green):编写最少量的功能代码,使得测试通过(绿色)。
重构(Refactor):优化和重构代码,确保它仍然通过测试,并且更易于理解和维护。

TDD 的主要目标是通过测试来推动开发,确保代码的质量和可用性。它可以帮助开发人员更好地理解需求,并减少错误和缺陷。此外,TDD 还提供了快速反馈机制,让开发人员及早发现和解决问题。最终,这种开发方法可以提高代码的可维护性和可扩展性,并加速整个开发过程。

先编写测试合约
测试合约报错
实现测试合约里的功能
再次测试
成功
重构,完善代码

实践
功能设计
1.可以查看总共有多少信件
2.当有新的信件到来时,总信件数 + 1
3.存储信件内容并可查看
4.存储信件发送人并可查看

先编写测试合约【还未新建合约】
在这里插入图片描述

npx hardhat test
失败
在这里插入图片描述
红灯

新建合约Mailbox.sol
在这里插入图片描述
npx hardhat test
成功绿灯
在这里插入图片描述
1.可以查看总共有多少信件
在这里插入图片描述
npx hardhat test
失败
在这里插入图片描述
实现这个功能
在这里插入图片描述
npx hardhat test
成功绿灯
在这里插入图片描述

最终的合约

// SPDX-License-Identifier: MIT 
pragma solidity ^0.8.9;

contract Mailbox{
    uint public totalLetters;
    struct Letter{
        string letter;
        address sender;
    }
    Letter[] public letters;


    function write(string memory letter) public{
        totalLetters++;
        letters.push(Letter(letter,msg.sender));
    }

    function get() public view returns(Letter[] memory){
        return letters;
    }
}

测试代码

const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("Mailbox",async()=>{
    it("should get mailbox contract",
    async() => {
        const mailboxContract = await
        ethers.getContractFactory("Mailbox");
    });
    
    it("should get total letters in the box",
    async() => {
        const mailboxContract = await
        ethers.getContractFactory("Mailbox");
        
        const mailbox = await
        mailboxContract.deploy();
        
        expect(await mailbox.totalLetters()).to.equal(0);//测试totalLetters变量
    });

    it("should increase by one when get new letter",
    async() => {
        const mailboxContract = await ethers.getContractFactory("Mailbox");//获取合约
        
        const mailbox = await mailboxContract.deploy();//部署合约
        
        await mailbox.write("hello");//测试write方法

        expect(await mailbox.totalLetters()).to.equal(1);


    });

    it("should get mail contents",
    async() => {
        const mailboxContract = await ethers.getContractFactory("Mailbox");//获取合约
        
        const mailbox = await mailboxContract.deploy();//部署合约

        await mailbox.write("hello");//测试write方法
        
        const letters = await mailbox.get();

        expect(letters[0].letter).to.equal("hello");//测试write方法是否写入

    });

    it("should get mail sender",
    async() => {
        const mailboxContract = await ethers.getContractFactory("Mailbox");//获取合约
        
        const mailbox = await mailboxContract.deploy();//部署合约

        await mailbox.write("hello");//测试write方法
        
        const letters = await mailbox.get();

        expect(letters[0].sender).to.equal("改成你的地址");//测试write方法是否写入
        
    });

});
  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值