当前位置: 首页 > news >正文

有哪些简单的网站建设部网站资质查询

有哪些简单的网站,建设部网站资质查询,网站推广软文欣赏,代驾软件系统多少钱一套喜欢的话别忘了点赞、收藏加关注哦#xff0c;对接下来的教程有兴趣的可以关注专栏。谢谢喵#xff01;(#xff65;ω#xff65;) 11.4.1. 验证错误处理的情况 测试函数出了验证代码的返回值是否正确#xff0c;还需要验证代码是否如预期的去处理了发生错误的情况。比…喜欢的话别忘了点赞、收藏加关注哦对接下来的教程有兴趣的可以关注专栏。谢谢喵(ω) 11.4.1. 验证错误处理的情况 测试函数出了验证代码的返回值是否正确还需要验证代码是否如预期的去处理了发生错误的情况。比如说可以编写一个测试来验证代码是否在特定情况下发生了panic!。 这种测试需要为函数额外增加should_panic属性。使用它标记的函数如果在函数内发生了恐慌则代表通过测试反之就失败。 看个例子 pub struct Guess {value: i32, }impl Guess {pub fn new(value: i32) - Guess {if value 1 || value 100 {panic!(Guess value must be between 1 and 100, got {value}.);}Guess { value }} }#[cfg(test)] mod tests {use super::*;#[test]#[should_panic]fn greater_than_100() {Guess::new(200);} }结构体Guess有一个存储u32类型数据的字段value它有一个关联函数new用于创建一个Guess实例但前提是传进new的参数大于1小于100否则就要恐慌。greater_than_100这个测试函数测试给new函数传入大于100的值这时候应该发生恐慌所以为这个测试函数添加了一个should_panic的attribute(属性)也就是写#[should_panic]。 测试结果 $ cargo testCompiling guessing_game v0.1.0 (file:///projects/guessing_game)Finished test profile [unoptimized debuginfo] target(s) in 0.58sRunning unittests src/lib.rs (target/debug/deps/guessing_game-57d70c3acb738f4d)running 1 test test tests::greater_than_100 - should panic ... oktest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00sDoc-tests guessing_gamerunning 0 teststest result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s下面来人为引入bug把new函数里的value 100的判断去掉 pub struct Guess {value: i32, }impl Guess {pub fn new(value: i32) - Guess {if value 1 || value 100 {panic!(Guess value must be between 1 and 100, got {value}.);}Guess { value }} }#[cfg(test)] mod tests {use super::*;#[test]#[should_panic]fn greater_than_100() {Guess::new(200);} }这时候测试函数中的Guess::new(200);就不会恐慌但是因为它添加了should_panic这个attribute所以本应该恐慌的函数没有恐慌就会导致测试失败 $ cargo testCompiling guessing_game v0.1.0 (file:///projects/guessing_game)Finished test profile [unoptimized debuginfo] target(s) in 0.62sRunning unittests src/lib.rs (target/debug/deps/guessing_game-57d70c3acb738f4d)running 1 test test tests::greater_than_100 - should panic ... FAILEDfailures:---- tests::greater_than_100 stdout ---- note: test did not panic as expectedfailures:tests::greater_than_100test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00serror: test failed, to rerun pass --lib11.4.2. 让should_panic更精确 有的时候使用should_panic进行的测试会有点含糊不清因为它仅仅能够说明被检查的代码是否发生了恐慌即使这个恐慌和程序员预期的恐慌不一样。 为了使测试更精确可以为should_panic添加一个可选的expected参数。这样程序就会检查失败消息中是否包含所指定的文字。 看个例子 pub struct Guess {value: i32, }impl Guess {pub fn new(value: i32) - Guess {if value 1 {panic!(Guess value must be greater than or equal to 1, got {value}.);} else if value 100 {panic!(Guess value must be less than or equal to 100, got {value}.);}Guess { value }} }#[cfg(test)] mod tests {use super::*;#[test]#[should_panic(expected less than or equal to 100)]fn greater_than_100() {Guess::new(200);} }在刚才的结构体上稍微进行了修改把new函数里value 1和value 100的情况分开写了两个不同的恐慌信息。给should_panic属性添加了expected参数后面跟的就是期待的报错信息。只有测试函数恐慌并且恐慌信息包括期待的报错信息才算测试成功否则就算失败。 这个程序肯定能成功。 一样的套路我们来手动引入错误比如我们把new函数里小于1和大于100的恐慌信息交换一下 pub struct Guess {value: i32, }impl Guess {pub fn new(value: i32) - Guess {if value 1 {panic!(Guess value must be less than or equal to 100, got {value}.);} else if value 100 {panic!(Guess value must be greater than or equal to 1, got {value}.);}Guess { value }} }#[cfg(test)] mod tests {use super::*;#[test]#[should_panic(expected less than or equal to 100)]fn greater_than_100() {Guess::new(200);} }测试结果 $ cargo testCompiling guessing_game v0.1.0 (file:///projects/guessing_game)Finished test profile [unoptimized debuginfo] target(s) in 0.66sRunning unittests src/lib.rs (target/debug/deps/guessing_game-57d70c3acb738f4d)running 1 test test tests::greater_than_100 - should panic ... FAILEDfailures:---- tests::greater_than_100 stdout ---- thread tests::greater_than_100 panicked at src/lib.rs:12:13: Guess value must be greater than or equal to 1, got 200. note: run with RUST_BACKTRACE1 environment variable to display a backtrace note: panic did not contain expected stringpanic message: Guess value must be greater than or equal to 1, got 200.,expected substring: less than or equal to 100failures:tests::greater_than_100test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00serror: test failed, to rerun pass --lib失败消息表明此测试确实发生了恐慌但是恐慌消息不包含less than or equal to 100预期字符串。在这种情况下我们确实收到的恐慌信息是 Guess value must be greater than or equal to 1, got 200.。根据这个就可以纠错。
http://www.dnsts.com.cn/news/223976.html

相关文章:

  • 南宁新站seo设计类专业要艺考吗
  • 做物流百度网站东莞网站建设(曼哈顿信科)
  • 网站的基本概念深圳公司网站建设设计
  • 淘宝流量网站大连发现2例阳性
  • 东莞网站建设管理福清手机网站建设
  • 中国企业网站建设现状如何解析到凡科建设的网站
  • 做商城网站企业泉州握旗公司网站建设
  • 做网站商城开发什么语言最快宿州网站推广
  • 网站建设策划书封面松江区网站制作与推广
  • 中国建设监理协会网站投稿移动端优秀网站
  • php网站开发常用框架网站建设公司哪家好智搜宝
  • 昆山网站设计南宁智慧园区网站建设
  • 前程无忧网广州网站建设类岗位竞价推广开户电话
  • 网站的设计原则有哪些网上购物最便宜的网站
  • 学设计网站推荐富海人才招聘网官网
  • wordpress 多模板惠州seo网站管理
  • 长沙哪里有网站推广优化短链接还原
  • 网站备案一次就可以了吧中国网上购物平台有哪些
  • 开发工程师网站开发工程师丹东网站建设平台
  • 营销型网站建设的概念企业名录大全查询
  • 博物馆网站建设方案不同网站建设报价单
  • 沈阳网站seoppt 做的最好的网站有哪些
  • 音乐类网站页面设计特点铁法能源公司网站
  • 男男床做第一次视频网站介绍邯郸的网页
  • 跟网站开发有关系的工作有哪些专业微网站建设公司首选公司哪家好
  • 网站备案 后期pptppt模板免费下载
  • 站内推广网站空间更换
  • 电商网站开发的目的是公众号开发价钱
  • 南宁百度网站建设公司2022世界500强企业排名
  • 成都个人网站制作做拼货商城网站