简体   繁体   English

使用rspec测试易于受到外部影响的代码

[英]Using rspec to test code prone to external infuence

I'm using rspec to test a code that may fail depending on the change of a site structure (the external influence I mentioned). 我正在使用rspec来测试可能会失败的代码,具体取决于站点结构的更改(我提到的外部影响)。 I would like to write an example that involves "should raise an error" but I'm not sure if rspec is the right tool to test code in such situations. 我想写一个涉及“应该引发错误”的示例,但是我不确定rspec是否是在这种情况下测试代码的正确工具。 Could someone point me in some direction? 有人可以指出我的方向吗?

Thanks in advance 提前致谢

You could write custom matchers 您可以编写自定义匹配器

Something like : 就像是 :

site.should_have_valid_structure

Spec::Matchers.define :have_structure
  match do |actual|
    actual.structure == Site::VALID_STRUCTURE
  end
end

Mock the external influence so you can test it properly (if the external influence is a Web page or other HTTP request, WebMock and VCR are great for this). 模拟外部影响,以便可以对其进行正确测试(如果外部影响是网页或其他HTTP请求,则WebMock和VCR非常适合此操作)。 Your tests should not rely on anything external functioning properly -- or improperly. 您的测试不应依赖任何外部功能正常-或不正确的功能。 See http://marnen.github.com/webmock-presentation/webmock.html for an overview I wrote last year. 有关我去年撰写的概述,请参见http://marnen.github.com/webmock-presentation/webmock.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM