咸宁住房和城乡规划建设局网站,交互式网站设计怎么做,龙岗做网站公司哪家好,百度爱采购关键词优化测试驱动开发(Test Driven Development)#xff0c;是一种不同于传统软件开发流程的新型的开发方法。它要求在编写某个功能的代码之前先编写测试代码#xff0c;然后只编写使测试通过的功能代码通过测试来推动整个开发的进行。这有助于编写简洁可用和高质量的代码#xff0c…测试驱动开发(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 awaitethers.getContractFactory(Mailbox);});it(should get total letters in the box,async() {const mailboxContract awaitethers.getContractFactory(Mailbox);const mailbox awaitmailboxContract.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方法是否写入});});