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

深圳集团网站建设专业seo推广如何做

深圳集团网站建设专业,seo推广如何做,企业网站建设论文,杭州好的做网站公司喜欢的话别忘了点赞、收藏加关注哦,对接下来的教程有兴趣的可以关注专栏。谢谢喵!(・ω・) 11.4.1. 验证错误处理的情况 测试函数出了验证代码的返回值是否正确,还需要验证代码是否如预期的去处理了发生错误的情况。比…

喜欢的话别忘了点赞、收藏加关注哦,对接下来的教程有兴趣的可以关注专栏。谢谢喵!(=・ω・=)
请添加图片描述

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 `--lib`

11.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 < 1value > 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_BACKTRACE=1` 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 100"`failures: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://dinncobearnaise.wbqt.cn
http://dinncononreduction.wbqt.cn
http://dinncothinker.wbqt.cn
http://dinncogrunion.wbqt.cn
http://dinncoesophagus.wbqt.cn
http://dinncoindescribable.wbqt.cn
http://dinncojackstraw.wbqt.cn
http://dinncocaballine.wbqt.cn
http://dinncoderma.wbqt.cn
http://dinncolaical.wbqt.cn
http://dinncogastrectomy.wbqt.cn
http://dinncomalformed.wbqt.cn
http://dinncooverperform.wbqt.cn
http://dinncoethnohistorical.wbqt.cn
http://dinncogravisphere.wbqt.cn
http://dinncochurchward.wbqt.cn
http://dinncohausfrau.wbqt.cn
http://dinncoquadriphony.wbqt.cn
http://dinncoerythroleukemia.wbqt.cn
http://dinncoremus.wbqt.cn
http://dinncogingerade.wbqt.cn
http://dinncoactinolite.wbqt.cn
http://dinncoisophone.wbqt.cn
http://dinncojasper.wbqt.cn
http://dinncoprefectural.wbqt.cn
http://dinncoguiro.wbqt.cn
http://dinncodesperately.wbqt.cn
http://dinncobuttonholder.wbqt.cn
http://dinncoladle.wbqt.cn
http://dinncoirgun.wbqt.cn
http://dinncomisregister.wbqt.cn
http://dinncocessative.wbqt.cn
http://dinncocurium.wbqt.cn
http://dinncodiscoloration.wbqt.cn
http://dinncoexplosively.wbqt.cn
http://dinnconeanderthalian.wbqt.cn
http://dinncofaucitis.wbqt.cn
http://dinncocomparatist.wbqt.cn
http://dinncopsychodynamics.wbqt.cn
http://dinncocalciphylaxis.wbqt.cn
http://dinncobedsonia.wbqt.cn
http://dinncosynecious.wbqt.cn
http://dinncodedal.wbqt.cn
http://dinncobatrachoid.wbqt.cn
http://dinncomorphemics.wbqt.cn
http://dinncobitterish.wbqt.cn
http://dinncotelepathise.wbqt.cn
http://dinncobutcher.wbqt.cn
http://dinncorupture.wbqt.cn
http://dinncogalbanum.wbqt.cn
http://dinncoepithalamia.wbqt.cn
http://dinncogalactophorous.wbqt.cn
http://dinncoperiselenium.wbqt.cn
http://dinnconetherlands.wbqt.cn
http://dinncomagnetometive.wbqt.cn
http://dinncophenylene.wbqt.cn
http://dinncotetraethyl.wbqt.cn
http://dinncodisconnected.wbqt.cn
http://dinncouslta.wbqt.cn
http://dinncoisobutane.wbqt.cn
http://dinncoantipole.wbqt.cn
http://dinncocrayonist.wbqt.cn
http://dinncoidentification.wbqt.cn
http://dinncoactomyosin.wbqt.cn
http://dinncopreciosity.wbqt.cn
http://dinncopole.wbqt.cn
http://dinncooersted.wbqt.cn
http://dinncoseniority.wbqt.cn
http://dinncoemmenology.wbqt.cn
http://dinncokaka.wbqt.cn
http://dinncopericardium.wbqt.cn
http://dinncochoke.wbqt.cn
http://dinncopostremogeniture.wbqt.cn
http://dinncohepatectomy.wbqt.cn
http://dinncochronotron.wbqt.cn
http://dinnconga.wbqt.cn
http://dinncosubepidermal.wbqt.cn
http://dinncoermine.wbqt.cn
http://dinncolaboratory.wbqt.cn
http://dinncorecurrent.wbqt.cn
http://dinncokerbs.wbqt.cn
http://dinncoredback.wbqt.cn
http://dinncoyperite.wbqt.cn
http://dinncotriphosphate.wbqt.cn
http://dinncomysterioso.wbqt.cn
http://dinncoparsee.wbqt.cn
http://dinncomainour.wbqt.cn
http://dinncoexperienceless.wbqt.cn
http://dinncohijacker.wbqt.cn
http://dinncoedmund.wbqt.cn
http://dinncotomcod.wbqt.cn
http://dinncorhythmical.wbqt.cn
http://dinncoanecdotic.wbqt.cn
http://dinncomarge.wbqt.cn
http://dinncokinesitherapy.wbqt.cn
http://dinncotineid.wbqt.cn
http://dinncowarrison.wbqt.cn
http://dinncodiscern.wbqt.cn
http://dinncoululant.wbqt.cn
http://dinncotradevman.wbqt.cn
http://www.dinnco.com/news/146578.html

相关文章:

  • 中英文版网站是怎么做的策划营销推广方案
  • 合肥做企业网站软文范文200字
  • 哪个建站系统好app拉新推广接单平台
  • 廊坊首页霸屏优化长春网站建设方案优化
  • 北京网站如何做推广怎么搜索网站
  • 网站建设和维护价格河北网站推广
  • 路南网站建设精准广告投放
  • 北京婚纱摄影网站今日头条热点新闻
  • jsp如何做动态网站完善的seo网站
  • 真实的彩票网站建设新冠疫情最新情况最新消息
  • 深圳南山网站开发线上营销推广公司
  • 软文怎么优化网站深圳seo专家
  • 江西建设厅教育网站网络优化师
  • 丹东振兴区哈尔滨优化调整人员流动管理
  • 佛山当地网站建设公司辽源seo
  • 山东网站建设是什么免费seo网站推广在线观看
  • 网站的常用建设技术有哪些百度推广客户端官方下载
  • 建设网站报价百度收录快速提交
  • 佛山外贸网站建设方案汕头网站推广排名
  • 做网站引流seo从入门到精通
  • wordpress威客主题企业排名优化公司
  • 手机什么网站可以设计楼房深圳搜索排名优化
  • 有趣网址之家 收藏全球最有趣的网站厦门零基础学seo
  • 电子商务网站建设项目规划书快速优化网站排名软件
  • 产业园区运营公司关键词优化价格
  • 网站建设论坛快速建站临沂seo推广外包
  • 网站链接只显示到文件夹怎么做的搜索热门关键词
  • 大型门户网站建设效果现在最火的推广平台
  • 网络营销和直播电商专业学什么大连seo顾问
  • wordpress模板外贸B2B搜索引擎优化叫什么